Re: PEP Idea: Real private attribute

2021-09-01 Thread Calvin Spealman
On Tue, Aug 31, 2021 at 1:19 PM Mehrzad Saremi wrote: > Calvin, even if the language offered truly private members? > I'm saying I don't think they're necessary, especially not for the use case posited here. Private members in other languages are about things internal to the class of the object

Re: PEP Idea: Real private attribute

2021-08-31 Thread Mehrzad Saremi
Calvin, even if the language offered truly private members? On Tue, 31 Aug 2021 at 17:31, Calvin Spealman wrote: > The right way for those decorators to hold some private information, imho, > isn't to put anything on the decorated object at all, but to use a weak-ref > dictionary using the targe

Re: PEP Idea: Real private attribute

2021-08-31 Thread Calvin Spealman
The right way for those decorators to hold some private information, imho, isn't to put anything on the decorated object at all, but to use a weak-ref dictionary using the target object as a key. On Sat, Aug 28, 2021 at 5:42 PM Mehrzad Saremi wrote: > Python currently uses name mangling for doub

Re: PEP Idea: Real private attribute

2021-08-29 Thread Mehrzad Saremi
The proposed semantics would be the same as self.__privs__[__class__, "foo"]; yes I can say the problem is ugliness. The following is an example where name mangling can be problematic (of course there are workarounds, yet if double-underscores are meant to represent class-specific members, the foll

Re: PEP Idea: Real private attribute

2021-08-29 Thread Chris Angelico
On Mon, Aug 30, 2021 at 5:49 AM Mehrzad Saremi wrote: > > No, a class ("the class that I'm lexically inside") cannot be accessed from > outside of the class. This is why I'm planning to offer it as a core > feature because only the parser would know. There's apparently no elegant > solution if you

Re: PEP Idea: Real private attribute

2021-08-29 Thread Mehrzad Saremi
No, a class ("the class that I'm lexically inside") cannot be accessed from outside of the class. This is why I'm planning to offer it as a core feature because only the parser would know. There's apparently no elegant solution if you want to implement it yourself. You'll need to write self.__privs

Re: PEP Idea: Real private attribute

2021-08-28 Thread Chris Angelico
On Sun, Aug 29, 2021 at 7:40 AM Mehrzad Saremi wrote: > > Python currently uses name mangling for double-underscore attributes. Name > mangling is not an ideal method to avoid name conflicting. There are > various normal programming patterns that can simply cause name conflicting > in double-under

PEP Idea: Real private attribute

2021-08-28 Thread Mehrzad Saremi
Python currently uses name mangling for double-underscore attributes. Name mangling is not an ideal method to avoid name conflicting. There are various normal programming patterns that can simply cause name conflicting in double-underscore members. A typical example is when a class is re-decorated

Re: Private attribute

2008-08-26 Thread Ken Starks
Steven D'Aprano wrote: def SomeClass(object): _gridsize = 0.8 The leading underscore tells callers that they change the attribute at their own risk. An even more Pythonic approach is to write your class that makes no assumptions about gridsize, and thus explicitly supports any reaso

Re: Private attribute

2008-08-25 Thread Steven D'Aprano
On Mon, 25 Aug 2008 19:47:38 +0100, Ken Starks wrote: > I have a class with an attribute called 'gridsize' and I want a derived > class to force and keep it at 0.8 (representing 8mm). > > Is this a correct, or the most pythonic approach? Others have already suggested using a property, but I'll s

Re: Private attribute

2008-08-25 Thread Ken Starks
Marc 'BlackJack' Rintsch wrote: On Mon, 25 Aug 2008 21:44:49 +0100, Ken Starks wrote: def __getattr__(self,attrname): if attrname == 'gridsize': return 0.8 def __setattr__(self,attrname,value): if attrname == 'gridsize':

Re: Private attribute

2008-08-25 Thread Marc 'BlackJack' Rintsch
On Mon, 25 Aug 2008 21:44:49 +0100, Ken Starks wrote: >>> >>> >>> def __getattr__(self,attrname): >>> if attrname == 'gridsize': >>> return 0.8 >>> >>> def __setattr__(self,attrname,value): >>> if attrname == 'gridsize': >>>

Re: Private attribute

2008-08-25 Thread Ken Starks
André wrote: On Aug 25, 3:47 pm, Ken Starks <[EMAIL PROTECTED]> wrote: I have a class with an attribute called 'gridsize' and I want a derived class to force and keep it at 0.8 (representing 8mm). Is this a correct, or the most pythonic approach? def __getattr__(self

Re: Private attribute

2008-08-25 Thread André
On Aug 25, 3:47 pm, Ken Starks <[EMAIL PROTECTED]> wrote: > I have a class with an attribute called 'gridsize' and I want > a derived class to force and keep it at 0.8 (representing 8mm). > > Is this a correct, or the most pythonic approach? > > > >      def __getattr__(self,at

Re: Private attribute

2008-08-25 Thread castironpi
On Aug 25, 2:09 pm, Ken Starks <[EMAIL PROTECTED]> wrote: > Ken Starks wrote: > > I have a class with an attribute called 'gridsize' and I want > > a derived class to force and keep it at 0.8 (representing 8mm). > > > Is this a correct, or the most pythonic approach? > > > > >

Re: Private attribute

2008-08-25 Thread Ken Starks
Ken Starks wrote: I have a class with an attribute called 'gridsize' and I want a derived class to force and keep it at 0.8 (representing 8mm). Is this a correct, or the most pythonic approach? def __getattr__(self,attrname): if attrname == 'gridsize':

Private attribute

2008-08-25 Thread Ken Starks
I have a class with an attribute called 'gridsize' and I want a derived class to force and keep it at 0.8 (representing 8mm). Is this a correct, or the most pythonic approach? def __getattr__(self,attrname): if attrname == 'gridsize': return 0.8