Why this code is working?

Mel mwilson at the-wire.com
Wed Jan 14 11:21:38 EST 2009


Hussein B wrote:

> On Jan 14, 11:55 am, Bruno Desthuilliers <bruno.
> 42.desthuilli... at 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.

You don't have to know about f2 when f1 is compiled.  You only have to know
about f2 when f1 is called.

I predict that

def f1():
    n = 88
    f2 (n)
f1()
def f2 (x):
    print x


will *not* work.  Python won't consider the non-definition of f2 to be a
problem until you try to actually use it.

        Mel.




More information about the Python-list mailing list