Function name inside function (was RE: End of file)

Robert Brewer fumanchu at amor.org
Mon Oct 11 01:02:19 EDT 2004


Andrew Durdin wrote:
> Another [sort-of related] question: why does the following not produce
> a NameError for "foo"?
> 
> def foo(): print foo
> foo()

At what point did you expect the NameError?

>>> def foo(): print foo
... 
>>> import dis
>>> dis.dis(foo)
  1           0 LOAD_GLOBAL              0 (foo)
              3 PRINT_ITEM          
              4 PRINT_NEWLINE       
              5 LOAD_CONST               0 (None)
              8 RETURN_VALUE        


The inner 'foo' isn't looked up until the function is called, at which
time, 'foo' is present in globals().


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list