Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

python - Confusion with properties

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From documentation:

property([fget[, fset[, fdel[, doc]]]])

Return a property attribute for new-style classes (classes that derive from object).

Descriptors are only invoked for new style objects or classes. Your using old-style classes. Inherits from a base type object:

class Test1(object):
    # your code

class Test2(object):
    def __init__(self):
        self._val = 0
    
    @property
    def val(self):
        return self._val
    
    @val.setter
    def val(self,newVal): # should be named as property
        self._val = newVal
    
    def __str__(self):
        return "Value: {}".format(self.val)

This work fine:

>>> verify(Test1())

Check with Test1
('Value check:', 0)
('Value after a.val = 5 is:', 5)
('The actual value is:', 5)

Read more about difference between new-style classes and classic classes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...