Eric Snow added the comment:
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/
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue24897>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com