Issue31441
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2017-09-13 08:38 by Benjamin Wohlwend, last changed 2022-04-11 14:58 by admin. This issue is now closed.
Messages (5) | |||
---|---|---|---|
msg302033 - (view) | Author: Benjamin Wohlwend (Benjamin Wohlwend) | Date: 2017-09-13 08:38 | |
The first descriptor example in the descriptor docs (https://docs.python.org/3/howto/descriptor.html#descriptor-example) stores the value on the descriptor instance, which is shared among all MyClass instances. This leads to surprising (and arguably buggy from a user perspective) behaviour: m1, m2 = MyClass(), MyClass() m1.x = 5 m2.x = 10 print(m1.x, m2.x) >>> 10 10 I'm not sure how this could be fixed without making the example much more complicated (e.g. by introducing a "values" weakref dictionary on the descriptor instance). Maybe pointing this behaviour out in the docs could be enough, although I don't see any useful use case for a class that has this behaviour. |
|||
msg302072 - (view) | Author: R. David Murray (r.david.murray) * | Date: 2017-09-13 13:09 | |
Here is a not-much-more-complicated version that solves the problem. It is probably worth changing as the revised example makes clear the difference between self and obj, which is an important distinction. class RevealAccess(object): """A data descriptor that sets and returns values normally and prints a message logging their access. """ def __init__(self, initval=None, name='var'): self.attrname = '_' + str(random.random())[2:] self.name = name self.initval = initval def __get__(self, obj, objtype): print('Retrieving', self.name) return getattr(obj, self.attrname, self.initval) def __set__(self, obj, val): print('Updating', self.name) setattr(obj, self.attrname, val) class MyClass: x = RevealAccess(10, 'var "x"') y = 5 |
|||
msg302142 - (view) | Author: Raymond Hettinger (rhettinger) * | Date: 2017-09-14 06:25 | |
The example should be modernized to show-off the new __set_name__() part of the descriptor protocol. Computing the name on-demand is better that relying an optional user entered alternate name and having to store that name for future reference. |
|||
msg407606 - (view) | Author: Irit Katriel (iritkatriel) * | Date: 2021-12-03 21:49 | |
see also issue20751. |
|||
msg407613 - (view) | Author: Raymond Hettinger (rhettinger) * | Date: 2021-12-03 22:58 | |
This example was removed and replaced with better examples. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:58:52 | admin | set | github: 75622 |
2021-12-03 22:58:42 | rhettinger | set | status: open -> closed resolution: out of date messages: + msg407613 stage: resolved |
2021-12-03 21:49:26 | iritkatriel | set | nosy:
+ iritkatriel messages: + msg407606 |
2017-09-14 06:25:59 | rhettinger | set | assignee: docs@python -> messages: + msg302142 |
2017-09-13 13:09:13 | r.david.murray | set | nosy:
+ rhettinger, r.david.murray messages: + msg302072 |
2017-09-13 08:38:27 | Benjamin Wohlwend | create |