我继承了一些python代码,其中包含一个相当神秘的装饰器.此装饰器在整个项目的类中设置属性.问题是,我已将此调试问题跟踪到此装饰器.似乎它"fubars"我试过的所有调试器,并试图加速psyco休息的代码.(似乎psyco和这个装饰师不好玩).我认为最好改变它.
def Property(function):
"""Allow readable properties"""
keys = 'fget', 'fset', 'fdel'
func_locals = {'doc':function.__doc__}
def probeFunc(frame, event, arg):
if event == 'return':
locals = frame.f_locals
func_locals.update(dict((k,locals.get(k)) for k in keys))
sys.settrace(None)
return probeFunc
sys.settrace(probeFunc)
function()
return property(**func_locals)
Run Code Online (Sandbox Code Playgroud)
像这样使用:
class A(object):
@Property
def prop():
def fget(self):
return self.__prop
def fset(self, value):
self.__prop = value
... ect
Run Code Online (Sandbox Code Playgroud)
我得到的错误说问题是因为sys.settrace.(也许这是滥用settrace?)
我的问题:没有 sys.settrace,是否可以实现相同的装饰器.如果不是,我正在进行一些重写.
我有一个房产装饰师所以:
def Property(f):
"""
Allow readable properties without voodoo.
"""
fget, fset, fdel = f()
fdoc = f.__doc__
return property(fget, fset, fdel, fdoc)
Run Code Online (Sandbox Code Playgroud)
使用(例如)所以:
@Property
def method():
""""""
def fget(self):
return some expression...
return fget, None, None
Run Code Online (Sandbox Code Playgroud)
所以我的问题是关于这样做的python方式.Pydev抱怨说
"方法
method应该有自己作为第一个参数"
而且pylint给了我
方法没有争论
我知道我可以在pydev中关闭此错误消息,但我想知道是否有更好的方法来管理不将self作为参数的方法,我可以做得更好.
decorator ×2
python ×2
c ×1
debugging ×1
literals ×1
properties ×1
pydev ×1
pylint ×1
python-3.x ×1