Weird behavior with lexical scope
Arnaud Delobelle
arnodel at googlemail.com
Thu Nov 6 12:27:55 EST 2008
mrstevegross <mrstevegross at gmail.com> writes:
> I ran into a weird behavior with lexical scope in Python. I'm hoping
> someone on this forum can explain it to me.
>
> Here's the situation: I have an Outer class. In the Outer class, I
> define a nested class 'Inner' with a simple constructor. Outer's
> constructor creates an instance of Inner. The code looks like this:
>
> =========
> class Outer:
> class Inner:
> def __init__(self):
> pass
> def __init__ (self):
> a = Inner()
> Outer()
> =========
>
> However, the above code doesn't work. The creation of Inner() fails.
This is because there isn't lexical scoping in class scopes.
Try replacing
a = Inner()
with
a = Outer.Inner()
Alternatively,
class Outer:
class Inner:
...
def __init__(self, Inner=Inner):
a = Inner()
HTH
--
Arnaud
More information about the Python-list
mailing list