[Tutor] globals( ) and binding doubt (newbie) !

Karthik Gurumurthy karthikg@aztec.soft.net
Tue, 8 Jan 2002 20:48:49 +0530


hi all,

please take a look at python session dump: I have put comments at
significant places.

>>> def f1():
...     print 'f1'
...
>>> def f2():
...     print 'f2'
...
>>> def f3():
...     print 'f3'
...
>>> l = [f1,f2,f3]
>>> map(lambda f:f(),l)
f1
f2
f3
[None, None, None]
>>> def f1(a):
...     print a
...
>>> def f2(a):
...     print a
...
>>> def f3(a):
...     print a
...
>>>
>>>
>>> seq = [f1,f2,f3]				##### Here seq is filled with f1,f2 and f3
>>>
>>> l = [1,2,3,4,5]
>>> map(lambda i:map(lambda f:f(i),seq),l)
1
1
1
2
2
2
3
3
3
4
4
4
5
5
5
[[None, None, None], [None, None, None], [None, None, None], [None, None,
None], [None, No
ne, None]]
>>> def f1(a):					##### changed the implementations of the functions
...     print "f1 " + str(a)			##### now globals would have bound the names
f1,f2 and f3 to the latest definitions
...
>>> def f2(b):
...     print "f2 " + str(b)
...
>>> def f3(c):
...     print "f3 " + str(c)
...
>>> map(lambda i:map(lambda f:f(i),seq),l) ##### But this seems to have made
the call on the earlier definition
1							 ##### seq is the list which the methods
1							 ##### did'nt work as expected
1
2
2
2
3
3
3
4
4
4
5
5
5
[[None, None, None], [None, None, None], [None, None, None], [None, None,
None], [None, No
ne, None]]
>>> seq
[<function f1 at 0x00780CE0>, <function f2 at 0x007817A0>, <function f3 at
0x00781150>]
>>> globals()
{'test1': <module 'test1' from 'test1.py'>, 'f1': <function f1 at
0x00786350>, 'f2': <func
tion f2 at 0x007839E0>, 'f3': <function f3 at 0x00780C70>, 'seq': [<function
f1 at 0x00780
CE0>, <function f2 at 0x007817A0>, <function f3 at 0x00781150>],
'__builtins__': <module '
__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, 'l': [1,
2, 3, 4, 5]}
>>>
>>>
>>>
>>> seq = [f1,f2,f3]     				##### I had to re-initialize the list with the
same names again.
>>> map(lambda i:map(lambda f:f(i),seq),l)
f1 1								##### Then things worked fine
f2 1
f3 1
f1 2
f2 2
f3 2
f1 3
f2 3
f3 3
f1 4
f2 4
f3 4
f1 5
f2 5
f3 5
[[None, None, None], [None, None, None], [None, None, None], [None, None,
None], [None, No
ne, None]]
>>>

Can someone tell me why it did'nt use the correct versions of the functions
earlier.
When python comes across a name, it does look up in locals() or globals()
and then executes the
one mapped there?

When i redefined f1 , f2 and f3, globals would have accepted these and
overwritten the earlier one?


thanks for your patience!
karthik