I am new to using properties, so I put together a simple test as shown below. In my tests, I made two classes "Test1" and "Test2" where each is meant to hold one value. I am attempting to use a property to govern access to the pseudo-hidden "val" attribute. This current test does not restrict any inputs or outputs of the "val" attribute as this program was only meant as a proof of concept. The two test classes shown below yield the same results and are supposed to represent the different methods to construct a property. The example uses for properties I am referring to are found on the python docs here.
As per the documentation:
If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.
where C is their test class. I thought that by setting the value the way I did would change _val and leave val as a property. However it seems to me that my method of accessing the properties setter is actually replacing the property with the integer 5
unless I am mistaken. I hope someone can clarify my confusion.
class Test1:
def __init__(self):
self._val = 0
def setVal(self,newVal):
self._val = newVal
val = property(lambda self: self._val, setVal, None, "Property for value")
def __str__(self):
return "Value: {}".format(self.val)
class Test2:
def __init__(self):
self._val = 0
@property
def val(self):
return self._val
@val.setter
def setVal(self,newVal):
self._val = newVal
def __str__(self):
return "Value: {}".format(self.val)
def verify(a):
print("
Check with {}".format(a.__class__.__name__))
print("Value check:",a.val)
a.val = 5
print("Value after a.val = 5 is:",a.val)
print("The actual value is:",a._val)
def main():
verify(Test1())
verify(Test2())
if __name__ == '__main__':
main()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…