Weird behavior with lexical scope | | |
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.
The error message looks like this:
File "/tmp/foo.py", line 12, in <module>
Outer()
File "/tmp/foo.py", line 10, in __init__
a = Inner()
NameError: global name 'Inner' is not defined
This surprises me! Since the construction of Inner takes place within
the lexical scope 'Outer', I assumed the interpreter would search the
Outer scope and find the 'Inner' symbol. But it doesn't! If I change:
a = Inner()
to
a = Outer.Inner()
it works fine, though.
So, can anyone explain to me how Python looks up symbols? It doesn't
seem to be searching the scopes I expected...
Thanks,
--Steve | | | | re: Weird behavior with lexical scope
mrstevegross <mrstevegross@gmail.comwrites: Quote:
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 | | | | re: Weird behavior with lexical scope
def __init__(self, Inner=Inner):
Ok, the Inner=Inner trick works. What the heck does that do, anyway?
I've never seen that formulation.
--Steve | | | | re: Weird behavior with lexical scope
I want to write a Vector class and it makes the most sense to just subclasslist. I also want to be able to instantiate a vector using either:
Vector( 1, 2, 3 )
OR
Vector( [1, 2, 3] )
so I have this:
class Vector(list):
def __new__( cls, *a ):
try:
print a
return list.__new__(cls, a)
except:
print 'broken'
return list.__new__(cls, list(a))
doing Vector( 1, 2, 3 ) on this class results in a TypeError - which doesn't seem to get caught by the try block (ie "broken" never gets printed, and it never tries to
I can do pretty much the exact same code but inheriting from tuple instead of list and it works fine.
is this a python bug? or am I doing something wrong?
thanks,
-h. | | | | re: Weird behavior with lexical scope Quote: Quote:
>def __init__(self, Inner=Inner):
SteveOk, the Inner=Inner trick works. What the heck does that do, anyway?
SteveI've never seen that formulation.
Understanding that will put you on the path to scoping enlightenment.
Consider when that default assignment is established and how that might
differ from the assignment that occurs when __init__ is called.
Skip | | | | re: Weird behavior with lexical scope
At 2008-11-06T16:57:39Z, mrstevegross <mrstevegross@gmail.comwrites: Quote:
class Outer:
class Inner:
def __init__(self):
pass
def __init__ (self):
a = Inner()
Outer()
Try instead:
class Outer:
def __init__(self):
a = self.Inner()
--
Kirk Strauser
The Day Companies | | | | re: Weird behavior with lexical scope
On Nov 6, 9:57*pm, mrstevegross <mrstevegr...@gmail.comwrote: Quote:
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.
The error message looks like this:
>
* File "/tmp/foo.py", line 12, in <module>
* * Outer()
* File "/tmp/foo.py", line 10, in __init__
* * a = Inner()
NameError: global name 'Inner' is not defined
>
This surprises me! Since the construction of Inner takes place within
the lexical scope 'Outer', I assumed the interpreter would search the
Outer scope and find the 'Inner' symbol. But it doesn't! If I change:
* a = Inner()
to
* a = Outer.Inner()
>
it works fine, though.
AFAIK, when 'Outer.__init__' executes, 'Inner' is first searched for
within 'Outer.__init__()'s local namespace. Since 'Inner' is defined
outside the function namespace, the search will fail. Python then
looks at the module level namespace - where Inner is again not defined
(only 'Outer' is available in the module namespace), the final search
will be in the interpreter global namespace which will fail too. When
you change your code from 'Inner' to 'Outer.Inner', the module level
namespace search will match ( or atleast that's how i think it should
all work :) )
Try this ..
class Outer:
def __init__(self):
class Inner:
def __init__(self): pass
a = Inner()
Outer()
This should work, because the Outer.__init__ namespace (first
namespace being searched) has Inner defined within it
-srp Quote:
>
So, can anyone explain to me how Python looks up symbols? It doesn't
seem to be searching the scopes I expected...
>
Thanks,
--Steve
| | | | re: Weird behavior with lexical scope
Hamish McKenzie <hamish@valvesoftware.comwrites: Quote:
I want to write a Vector class and it makes the most sense to just
subclass list. I also want to be able to instantiate a vector using
either:
>
Vector( 1, 2, 3 )
OR
Vector( [1, 2, 3] )
>
>
so I have this:
>
class Vector(list):
def __new__( cls, *a ):
try:
print a
return list.__new__(cls, a)
except:
print 'broken'
return list.__new__(cls, list(a))
>
>
doing Vector( 1, 2, 3 ) on this class results in a TypeError - which
doesn't seem to get caught by the try block (ie "broken" never gets
printed, and it never tries to
>
I can do pretty much the exact same code but inheriting from tuple
instead of list and it works fine.
>
is this a python bug? or am I doing something wrong?
You're doing something wrong!
When you call Vector.__new__(cls, *a), this creates a new list object
(call it L) and then calls Vector.__init__(L, *a) automatically, falling
back to list.__init__(L, *a) (which won't work when a has more than one
element). In fact list initialisation is done in list.__init__, not in
list.__new__ (as opposed to tuple initialisation because tuples are
immutable).
A solution: Quote: Quote: Quote:
>>class Vector(list):
.... def __init__(self, *args):
.... try:
.... list.__init__(self, *args)
.... except TypeError:
.... list.__init__(self, args)
.... [1, 2, 3] [1, 2, 3] [1] []
HTH
--
Arnaud | | | | re: Weird behavior with lexical scope saju.pillai@gmail.com wrote: Quote:
On Nov 6, 9:57 pm, mrstevegross <mrstevegr...@gmail.comwrote: Quote:
>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.
>The error message looks like this:
>>
> File "/tmp/foo.py", line 12, in <module>
> Outer()
> File "/tmp/foo.py", line 10, in __init__
> a = Inner()
>NameError: global name 'Inner' is not defined
>>
>This surprises me! Since the construction of Inner takes place within
>the lexical scope 'Outer', I assumed the interpreter would search the
>Outer scope and find the 'Inner' symbol. But it doesn't! If I change:
> a = Inner()
>to
> a = Outer.Inner()
>>
>it works fine, though.
So do that. Quote:
AFAIK, when 'Outer.__init__' executes, 'Inner' is first searched for
within 'Outer.__init__()'s local namespace. Since 'Inner' is defined
outside the function namespace, the search will fail. Python then
looks at the module level namespace - where Inner is again not defined
(only 'Outer' is available in the module namespace), the final search
will be in the interpreter global namespace which will fail too. When
Interpreter global namespace == builtins Quote:
you change your code from 'Inner' to 'Outer.Inner', the module level
namespace search will match ( or atleast that's how i think it should
all work :) )
Correct Why? Quote:
class Outer:
def __init__(self):
class Inner:
def __init__(self): pass
a = Inner()
This create a duplicate Inner class object for every instance of Outer,
which is almost certainly not what the OP wants. Quote:
Outer()
>
This should work, because the Outer.__init__ namespace (first
namespace being searched) has Inner defined within it
tjr | | | | re: Weird behavior with lexical scope
In message <mailman.3599.1226010740.3487.python-list@python.org>, Terry
Reedy wrote: Quote: saju.pillai@gmail.com wrote:
> Quote:
>class Outer:
> def __init__(self):
> class Inner:
> def __init__(self): pass
> a = Inner()
>
This create a duplicate Inner class object for every instance of Outer,
which is almost certainly not what the OP wants.
Does it matter? |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|