magicmethods
magicmethods copied to clipboard
Descriptor example doesn't handle multiple instances
If the user instantiates multiple Distance
objects, the descriptors will interfere with each other.
There are two (and-a-half) ways to fix this:
- Attach a
weakref.WeakKeyDictionary
to instances ofMeter
. Useinstance
as the key andvalue
as the value. This has the disadvantage thatDistance
must be hashable and probably should not override__eq__()
. - Place the information in an attribute of the
Distance
objects. This has the disadvantage of requiring cooperation between theDistance
class and theMeter
class. This requirement can be eased with the use of a metaclass, but never truly eliminated. - (variation of 2) Turn
Distance.meter
into a@property
(or raw attribute) and delete theMeter
class entirely.
thanks for the report @NYKevin, I will take a look soon
This is addressed in my pull request from last year. I used @NYKevin's option 2. I'm curious as to why my PR seems to have gone unnoticed.