I *have* used @property to suggest that variables be read-only.

That said, I believe that using the (I believe fairly common) pattern of
defining a property as a no-op wrapper around a corresponding "private"
(single underscore) attribute tends to be a more pythonic solution to the
read-only dilemma. Specifically, that pattern would be defining

@property
def value(self):
    return self._value

and then just not giving a setter property (or giving a setter that raises
NotImplementedError or something similar). In this way, I have made it
clear to users that they may use the property and can expect it to be there
in the future (it becomes easily documented, and also it offers more
guarantees than the underscore "private" attribute). At the same time, I've
told the user that it shouldn't be messed with, but any user *could *go in
and do what they want with the _value attribute, enabling folks like Andre
to make customizations as desired—but at their own risk.

Since this pattern is pretty easy to implement, and I think its more
pythonic, I'd opt to keep things as they are.

On Thu, May 6, 2021 at 11:10 AM Chris Angelico <ros...@gmail.com> wrote:

> On Fri, May 7, 2021 at 4:04 AM Shreyan Avigyan
> <pythonshreya...@gmail.com> wrote:
> >
> > Chris:
> >
> > > That would require some definition of what's "within" and what's
> > > "outside" the class, and whatever definition you use, it won't work
> > > with all forms of dynamic code.
> >
> > Yes, implementing that will be hard. But the question is I can't quite
> understand why this is not acceptable by the Python community? Private
> members may be a disaster but I don't think readonly attributes is. In fact
> that's what have been implemented for years using @property.
> >
>
> I'm not sure about other people, but I have never, not once, used
> @property as a means of controlling access. So giving me another way
> to do something that I am not, and don't want to, do... isn't much of
> an argument. :)
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/XDTYC7RAXP4KPKBPX24VTCYGSRDJGABX/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HBGDMB6SVUBAXAI37BZZUIQPHT73RUSQ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to