class-private names and the Zen of Python

Ned Batchelder ned at nedbatchelder.com
Tue Oct 8 07:07:53 EDT 2013


On 10/8/13 6:13 AM, Marco Buttu wrote:
> In the following case:
>
> >>> class Foo:
> ...     _Foo__a = 100
> ...     __a = 33
> ...
> >>> Foo._Foo__a
> 33
>
> I think this behavior, for a user who does not know the convention, 
> could be a surprise. Should be raising an exception (in order to 
> inform the user the transformation of the name __a have been replaced 
> an existing name) a possible --explicit-- alternative? 

You also get a "problem" if you do this:

     >>> class Foo:
    ...     a = 100
    ...     a = 33
    ...
     >>> Foo.a
    33

Or for that matter:

     >>> a = 100
     >>> a = 33
     >>> a
    33

There are lots of ways to change what value a name refers to, it's not 
an error to reassign names.

Also, as Terry mentions, no one has ever assigned the two names you 
show, so why try to warn about it?

--Ned.



More information about the Python-list mailing list