Python装饰器使用起来很有趣,但由于参数传递给装饰器的方式,我似乎已经碰壁了.这里我有一个装饰器被定义为基类的一部分(装饰器将访问类成员,因此它将需要self参数).
class SubSystem(object):
def UpdateGUI(self, fun): #function decorator
def wrapper(*args):
self.updateGUIField(*args)
return fun(*args)
return wrapper
def updateGUIField(self, name, value):
if name in self.gui:
if type(self.gui[name]) == System.Windows.Controls.CheckBox:
self.gui[name].IsChecked = value #update checkbox on ui
elif type(self.gui[name]) == System.Windows.Controls.Slider:
self.gui[name].Value = value # update slider on ui
...
Run Code Online (Sandbox Code Playgroud)
我省略了其余的实现.现在这个类是将继承它的各种SubSystems的基类 - 一些继承的类需要使用UpdateGUI装饰器.
class DO(SubSystem):
def getport(self, port):
"""Returns the value of Digital Output port "port"."""
pass
@SubSystem.UpdateGUI
def setport(self, port, value):
"""Sets the value of Digital Output port "port"."""
pass
Run Code Online (Sandbox Code Playgroud)
我再次省略了函数实现,因为它们不相关.
简而言之,问题是虽然我可以通过将其指定为SubSystem.UpdateGUI来从继承类访问基类中定义的装饰器,但在尝试使用它时我最终会得到此TypeError: …
假设我有一些基类:
class Task:
def run(self):
#override this!
Run Code Online (Sandbox Code Playgroud)
现在,我希望其他人继承Task并覆盖run()方法:
class MyTask(Task):
def run(self):
#successful override!
Run Code Online (Sandbox Code Playgroud)
但是,问题是必须在每个继承Task的类的run()方法之前和之后发生逻辑.
似乎我可以这样做的一种方法是在基类中定义另一个方法,然后调用run()方法.但是,我想问一下,有没有办法用装饰器来实现这个目的?这样做的最pythonic方式是什么?
我无法弄清楚为什么这个程序失败了.
#!/usr/bin/env python
from __future__ import division, print_function
from future_builtins import *
import types
import libui as ui
from PyQt4 import QtCore
import sip
p = ui.QPoint()
q = QtCore.QPoint()
def _q_getattr(self, attr):
print("get %s" % attr)
value = getattr(sip.wrapinstance(self.myself(), QtCore.QPoint), attr)
print("get2 %s returned %s" % (attr, value))
return value
p.__getattr__ = types.MethodType(_q_getattr, p)
print(p.__getattr__('x')()) # Works! Prints "0"
print(p.x()) # AttributeError: 'QPoint' object has no attribute 'x'
Run Code Online (Sandbox Code Playgroud)
我使用Boost.Python来创建libui,它暴露了类QPoint.我包括PyQt4,它有一个sip暴露的QPoint.我正在尝试完成两种类型之间的映射.
我检查过这p是一个新式的课程,为什么不__getattr__被要求p.x()?
我正在尝试使用未实现的方法编写一个抽象类,这将强制继承子级在重写方法(在装饰器中定义)时返回特定类型的值。
当我使用下面显示的代码时,子方法不会调用装饰器。我认为这是因为该方法被覆盖,这很有意义。我的问题基本上是这样的:有没有办法通过方法覆盖使装饰器持久化?
我不反对使用装饰器以外的东西,但这是一个很快想到的解决方案,我很想知道是否有任何方法可以使它起作用。
如果使用装饰器是正确且可能的选择,它看起来像这样:
def decorator(returntype):
def real_decorator(function):
def wrapper(*args, **kwargs):
result = function(*args, **kwargs)
if not type(result) == returntype:
raise TypeError("Method must return {0}".format(returntype))
else:
return result
return wrapper
return real_decorator
Run Code Online (Sandbox Code Playgroud)
我需要我的父类看起来类似于这个:
class Parent(ABC):
@decorator(int)
@abstractmethod
def aye(self, a):
raise NotImplementedError
Run Code Online (Sandbox Code Playgroud)
子类会做这样的事情:
class Child(Parent):
def aye(self, a):
return a
Run Code Online (Sandbox Code Playgroud)
如果需要,我很乐意更好地澄清我的问题,并感谢所有花时间提前阅读此问题的人!
python ×4
decorator ×2
inheritance ×2
boost-python ×1
ironpython ×1
pyqt ×1
python-3.x ×1
python-sip ×1