I'm trying to use new-style properties declaration: Show
and see the following in console:
what am I doing wrong? P.S.: Old-style declaration works fine.
tbodt 16.1k6 gold badges57 silver badges82 bronze badges asked Nov 15, 2010 at 10:27
Maxim PopravkoMaxim Popravko 4,0212 gold badges28 silver badges42 bronze badges 1 The documentation says the following about using decorator form of
I have no idea why this is since if you use So you need to change your code to the following:
John Y 13.6k1 gold badge45 silver badges71 bronze badges answered Nov 15, 2010 at 10:38
David WebbDavid Webb 187k57 gold badges308 silver badges298 bronze badges 5 The setter method has to have the same name as the getter. Don't worry, the decorator knows how to tell them apart.
answered Nov 15, 2010 at 10:31
When you call @x.setter, @x.getter, or @x.deleter, you're creating a new property object and giving it the name of the function you're decorating. So really, all that matters is that the last time you use a @x.*er decorator in the class definition, it has the name you actually want to use. But since this would leave your class namespace polluted with incomplete versions of the property you wish to use, it's best to use the same name to clean them up. answered Nov 1, 2011 at 22:24
If you don't want the extra
answered Apr 30, 2018 at 19:58
TcllTcll 6,9061 gold badge19 silver badges22 bronze badges Not the answer you're looking for? Browse other questions tagged python or ask your own question.What is property () in Python?In Python, property() is a built-in function that creates and returns a property object. The syntax of this function is: property(fget=None, fset=None, fdel=None, doc=None)
How do you create a property in Python?attr , then Python automatically calls fget() . If you assign a new value to the attribute, as in obj. attr = value , then Python calls fset() using the input value as an argument.
...
Getting Started With Python's property(). How do you override a property in Python?Two ways to do this are:. Subclass property. First way to do this is by subclassing the builtin property and adding decorators that are versions of getter , setter and/or deleter that extend the current get, set and delete callbacks. ... . Overwrite getter, setter and/or deleter.. How do you make a Python property readIf you need to make a read-only attribute in Python, you can turn your attribute into a property that delegates to an attribute with almost the same name, but with an underscore prefixed before the its name to note that it's private convention.
|