在將其標記爲重複項之前,我之前知道此question has been answered,但提供的解決方案似乎不適用於我的案例。我試圖以編程方式設置類屬性。我知道我可以使用property
對於這一點,所以我想這樣做的:返回屬性對象的類屬性
class Foo:
def __init__(self, x):
self._x = x
def getx(): return self._x
def setx(y): self._x = y
self.x = property(fget=getx, fset=setx)
然而,當我運行這個交互,我得到:
>>> f = Foo(42)
>>> f.x
<property object at 0x0000000>
>>> f._x
42
>>> f.x = 1
>>> f.x
1
有什麼辦法解決?
編輯:
我覺得我可能已經離開了太多,所以這裏是什麼,我其實是想達到。我有一個名爲config
的類變量,其中包含要設置爲屬性的配置值。類應該被繼承來實現config
變量:
class _Base:
config =()
def __init__(self, obj, **kwargs):
self._obj = obj()
for kwarg in kwargs:
# Whatever magic happens here to make these properties
# Sample implementation
class Bar(_Base):
config = (
"x",
"y"
)
def __init__(self, obj, x, y):
super().__init__(obj, x=x, y=y)
現在允許操作:
>>> b = Bar(x=3, y=4)
>>> b.x
3
>>> # Etc.
我試圖保持這種儘可能的乾燥,因爲我有子類_Base
很多。
你不是在課堂上設置它,而是將它設置在對象上。描述符不會像那樣工作。 –