using identifiers before they are defined

J

Julio Sergio

I'm puzzled with the following example, which is intended to be a part of a
module, say "tst.py":

a = something(5)

def something(i):
return i



When I try:

->>> import tst

The interpreter cries out:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "tst.py", line 11, in <module>
a = something(5)
NameError: name 'something' is not defined

I know that changing the order of the definitions will work, however there are
situations in which referring to an identifier before it is defined is
necessary, e.g., in crossed recursion.

So I modified my module:

global something

a = something(5)


def something(i):
return i


And this was the answer I got from the interpreter:

->>> import tst

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "tst.py", line 12, in <module>
a = something(5)
NameError: global name 'something' is not defined


Do you have any comments?

Thanks,

--Sergio.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top