python 属性装饰器_python中的属性装饰器初始化

{是否有人知道正则性是否有细微差别?

在python中使用@property修饰符时,属性初始化是否会出现任何问题?

我的理解是,这个decorator允许一个属性在每次被调用时都被函数计算,因为它是一个依赖于其他可变属性的属性。

我在中编写了一些@property修饰符,但是由于某些原因它们不能工作。

我收到了这样一个错误:

for key in temp_spectra.overall_properties_dictionary:

AttributeError: 'Spectra' object has no attribute 'overall_properties_dictionary'

据我所知,创建这些@property的正确方法如下:from PyQt4 import QtCore, QtGui

class someObject(QtCore.QObject):

def __init__(self, x, y):

self.x = x

self.y = y

@property

def some_other_property(self):

# some complicated function which will recalculate everytime it is called

# because the other properties may be mutable

p = self.x + self.y

return p

class otherObject(QtGui.QTabWidget):

def __init__(self, someObject):

print someObject.some_other_property

otherObject(someObject(1,5))

然而,这是有效的!所以我不确定代码的哪一部分会导致这种情况。

值得一提的是,我正在转换代码以利用multiprocessing。这会引起问题吗?我只是不明白初始化会出什么问题导致这种类型的错误。在

编辑:我按照建议更改了代码以匹配新样式的类。*

你可能感兴趣的:(python,属性装饰器)