Inserting class namespace into method scope

Günther Dietrich gd.usenet at
Sat Nov 20 17:45:32 CET 2010

Steven D'Aprano <steve at> wrote:

>I find myself having need of a class where the class scope is included in 
>the scope of methods in the class. A simple example from Python 3.1:
>x = "outside"
>class Magic:
>    x = "inside"
>    def method(self):
>        return x
>I would like Magic().method() to return "inside" rather than "outside".

Try this instead:
    return self.x

Best regards,


More information about the Python-list mailing list