Hussein B a écrit :
On Jan 14, 11:55 am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalid> wrote:
Hussein B a écrit :
Hey,
Why this code is working?
def f1( ):
... x = 88
... f2(x)
...
def f2(x):
... print x
...
f1( )
88
Well... Because it is correct ?
What make you think it _shouldn't_ work ?
Because def2 is defined after def1 in an interpreted language, not
compiled.
CPython actually compiles to byte-code, which is then executed.
But anyway: even if it was fully interpreted, the fact that f2 is
defined after f1 should not matter - what matters is that name f2 exists
(and is bound to a callable taking a single mandatory argument) when f1
is actually _called_.
>>> def f1():
... x = 42
... f2(x)
...
>>> f1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f1
NameError: global name 'f2' is not defined
>>> def f2(x):
... print x
...
>>> f1()
42
>>> def f2(x):
... print "pikaboo"
...
>>> f1()
pikaboo
>>>
>>> del f2
>>> f1()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in f1
NameError: global name 'f2' is not defined
>>>
HTH
--
http://mail.python.org/mailman/listinfo/python-list