Message248910
That said...
What's the benefit of it being a decorator? The docstring? Access to func.__name__? It could just as well be:
class attribute:
_name = None
def __get__(self, instance, owner):
if instance is None:
return self
if self._name is None:
for name, attr in vars(owner).items():
if attr is self:
self._name = name
break
return instance.__dict__[self._name]
However, note that this is a non-data descriptor since it lacks __set__ and __delete__. That means it is *not* a read-only wrapper like property. If that's not a concern then there's no point to using a descriptor at all since you can just use a constant as a place-holder:
class Bar:
x = ...
def __init__(self):
self.x = 42
If you *are* looking for a read-only wrapping descriptor then you'll need __set__ and __delete__ methods, likely ones that simply raise AttributeError.
FWIW, there are definitely plenty of useful things you can do with descriptors. [1] Some could be useful enough to make it into the stdlib (or builtins), but I'm not convinced the one you are proposing has enough justification at this point.
[1] https://bitbucket.org/ericsnowcurrently/presentations/src/default/utpy-may2015/ |
|
Date |
User |
Action |
Args |
2015-08-20 18:09:25 | eric.snow | set | recipients:
+ eric.snow, skip.montanaro, rhettinger, r.david.murray, ethan.furman, abarry |
2015-08-20 18:09:25 | eric.snow | set | messageid: <1440094165.79.0.966428260565.issue24897@psf.upfronthosting.co.za> |
2015-08-20 18:09:25 | eric.snow | link | issue24897 messages |
2015-08-20 18:09:25 | eric.snow | create | |
|