Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python
using identifiers before they are defined
 

Index | Next | Previous | View Flat


juliosergio at gmail

Jun 12, 2012, 10:53 AM


Views: 440
Permalink
using identifiers before they are defined

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.




--
http://mail.python.org/mailman/listinfo/python-list

Subject User Time
using identifiers before they are defined juliosergio at gmail Jun 12, 2012, 10:53 AM
    Re: using identifiers before they are defined josehmartinezz at gmail Jun 12, 2012, 11:16 AM
    Re: using identifiers before they are defined emile at fenx Jun 12, 2012, 11:17 AM
    Re: using identifiers before they are defined python at mrabarnett Jun 12, 2012, 11:25 AM
    Re: using identifiers before they are defined juliosergio at gmail Jun 12, 2012, 11:33 AM
        Re: using identifiers before they are defined malaclypse2 at gmail Jun 12, 2012, 11:36 AM
        Re: using identifiers before they are defined ethan at stoneleaf Jun 12, 2012, 11:51 AM
            Re: using identifiers before they are defined josehmartinezz at gmail Jun 12, 2012, 12:02 PM
            Re: using identifiers before they are defined juliosergio at gmail Jun 12, 2012, 1:25 PM
                Re: using identifiers before they are defined ethan at stoneleaf Jun 12, 2012, 1:46 PM
    Re: using identifiers before they are defined driscoll at cs Jun 12, 2012, 11:33 AM
    Re: using identifiers before they are defined ben+python at benfinney Jun 12, 2012, 8:33 PM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.