variable scope

Mark Dickinson dickinsm at gmail.com
Mon Sep 28 04:59:47 EDT 2009


On Sep 28, 9:37 am, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
> Joel Juvenal Rivera Rivera a écrit :
>
>
>
> > Yeah i forgot the self an try the code then i see
> > an error that it was not defines _uno__a so that's
> > where i define the global and see that behavior.
>
> (snip)
> >> Joel Juvenal Rivera Rivera wrote:
> >>> Hi i was playing around with my code the i realize of this
>
> >>> ###################
> >>> _uno__a = 1
> >>> class uno():
> >>>     __a = 2
> >>>     def __init__(self):
> >>>         print __a
> >>> uno()
> >>> ###################
> >>> and prints 1
>
> Looks like a bug to me. I Think you should fill a ticket...

I don't think it's a bug.  Unless I'm missing something,
it's the 'names in class scope are not accessible' gotcha,
described and justified in the 'Discussion' section of PEP 227

http://www.python.org/dev/peps/pep-0227/

and somewhat more briefly in the reference manual:

http://docs.python.org/reference/executionmodel.html#naming-and-binding

The double underscores and name mangling are a red herring:
you can get the same effect with:

b = 1
class uno(object):
    b = 2
    def __init__(self):
        print b

uno()

--
Mark



More information about the Python-list mailing list