如何将 @属性 发送到另一个 class,它可以使用它来获取 Python 中 属性 的实际值
How send @property to another class which can work with it to get the actual value of property in Python
这是我解决问题的方法。但我希望有一种方法更简单,更漂亮。我对 Python 还不是很了解,但我没有得到这个问题的任何答案——问题和答案是关于另一个问题的。也许我看起来很糟糕,或者有一种完全不同的方法不需要参考。我需要将 link 传递给 属性 的 class 始终可以独立接收其值,而无需接收额外的提醒,而且直接值。
class SomeObject:
def __init__(self):
self._someproperty =None
self.somepropertyRef=SomeObject.__dict__['someproperty'] # reference
@property # I need pass property as a parameter like a function
def someproperty(self):
return self._someproperty
@someproperty.setter
def someproperty(self, someproperty):
self._someproperty = someproperty
so = SomeObject()
# 1) example without using ref
so.someproperty="123"
temp = so.someproperty
print(temp == so.someproperty) # True
so.someproperty=0
print(temp == so.someproperty) # Of course, False!
# 2) example with using ref
temp = (so.somepropertyRef, so) # Oh...
print(temp[0].fget(temp[1]) == so.someproperty) # True. But long way...
so.someproperty="456"
print(temp[0].fget(temp[1]) == so.someproperty) # True. How to do the same easy?
SomeObject.someproperty
是您要查找的参考。属性是 class 具有特殊行为的属性,除其他外,从 class.
的实例调用时
temp = (SomeObject.someproperty, so)
如果您只想访问该值,您还可以保存对 SomeObject.someproperty.fget
的引用。
temp = (SomeObject.someproperty.fget, so)
print(temp[0](temp[1]) == so.someproperty)
so.foo = 456
print(temp[0](temp[1]) == so.someproperty)
这是我解决问题的方法。但我希望有一种方法更简单,更漂亮。我对 Python 还不是很了解,但我没有得到这个问题的任何答案——问题和答案是关于另一个问题的。也许我看起来很糟糕,或者有一种完全不同的方法不需要参考。我需要将 link 传递给 属性 的 class 始终可以独立接收其值,而无需接收额外的提醒,而且直接值。
class SomeObject:
def __init__(self):
self._someproperty =None
self.somepropertyRef=SomeObject.__dict__['someproperty'] # reference
@property # I need pass property as a parameter like a function
def someproperty(self):
return self._someproperty
@someproperty.setter
def someproperty(self, someproperty):
self._someproperty = someproperty
so = SomeObject()
# 1) example without using ref
so.someproperty="123"
temp = so.someproperty
print(temp == so.someproperty) # True
so.someproperty=0
print(temp == so.someproperty) # Of course, False!
# 2) example with using ref
temp = (so.somepropertyRef, so) # Oh...
print(temp[0].fget(temp[1]) == so.someproperty) # True. But long way...
so.someproperty="456"
print(temp[0].fget(temp[1]) == so.someproperty) # True. How to do the same easy?
SomeObject.someproperty
是您要查找的参考。属性是 class 具有特殊行为的属性,除其他外,从 class.
temp = (SomeObject.someproperty, so)
如果您只想访问该值,您还可以保存对 SomeObject.someproperty.fget
的引用。
temp = (SomeObject.someproperty.fget, so)
print(temp[0](temp[1]) == so.someproperty)
so.foo = 456
print(temp[0](temp[1]) == so.someproperty)