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:
Emanuel Barry added the comment:
I figured. I guess it makes more sense to do that on a per-library basis. Eric
does have a valid point, and perhaps this addition is not needed :) I realize
now that this isn't really needed, backing off and closing :)
--
Eric Snow added the comment:
This sort of thread belongs on python-id...@python.org, not on the tracker.
Please post there (feel free to reference this issue; also add a link to the
thread here). TBH, I think there's a larger discussion to be had regarding the
topic of other useful
R. David Murray added the comment:
So you are basically wanting to be able to create read only attributes that
don't have a _ in front of them even internally.
I don't think that's something Python core should add. Read only attributes
should be an exceptional case in most python programs.
Skip Montanaro added the comment:
I'm missing the point too, I think. I don't see class attributes in your
initial Foo class, then your second one as a __new__ method, but references
self. I'm quite confused at this point.
--
nosy: +skip.montanaro
Eric Snow added the comment:
No worries, Emanuel. Thanks for bringing it up. I'd still be interested to
see what sort discussion ensued if you took this to python-ideas. Starting a
thread on the topic has been on my todo list for a while but other matters
always end up taking precedence.
New submission from Emanuel Barry:
This is an issue that came up quite often when creating code where you want the
class' namespace to hold the instance attributes. I've often seen (and written)
code like this:
class Foo:
def __init__(self):
self._x = 42
@property
def x(self):
Ethan Furman added the comment:
Could you give an actual use-case demo, and how it's different from @property?
Could just be that I'm tired, but I'm not seeing the advantages of @attribute.
--
nosy: +ethan.furman
___
Python tracker
Emanuel Barry added the comment:
The only significant difference is that it lets the instance overwrite the
attribute (it doesn't have __set__ or __delete__). For example (using
fractions.Fraction to demonstrate), the following:
def __new__(cls, numerator=0, denominator=None,