[Python-ideas] Decorators for variables
Chris Angelico
rosuav at gmail.com
Sun Apr 3 03:20:13 EDT 2016
On Sun, Apr 3, 2016 at 4:53 PM, Matthias welp <boekewurm at gmail.com> wrote:
>> > var = property(setter, getter, deleter, docs)
>> > var = 20
>>
>> Can you explain - or, preferably, demonstrate - the difference you're
>> talking about here?
>
> Sorry, that was untested code. My expectations of class definitions was
> wrong, as it does not actually change behaviour inside it's own scope. I
> thought that when you are defining a class, that when you assign a property
> value to an attribute, that the attribute 'name value' will directly change
> it's behaviour to include the descriptor properties of the property object
> assigned. My mistake.
>
Ah. There is a significant difference between assignment within a
class definition and assignment from a function _inside_ that class
definition, but in any given scope, double assignment always does the
same thing: last one wins. Which is a good thing, when it comes to the
@property decorator:
class LifeAndUniverse:
@property
def answer(self):
return 42
@answer.setter
def answer(self, value):
print("No fair changing the answer!")
@answer.deleter
def answer(self):
print("You just deleted.... everything.")
Each function definition overwrites the previous "answer" with a new
one, which (thanks to the way setter and deleter are implemented)
incorporates the previous code, but nothing in Python mandates that.
So is there anything left of the assignment-decorator proposal, or is
it completely withdrawn? (I always like to read over even the bad
proposals - there's often something good in them, Martin Farquhar
Tupper's "Proverbial Philosophy" aside.)
ChrisA
More information about the Python-ideas
mailing list