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
Re: using identifiers before they are defined
You should define the function first and then call it. def something(i): return i a = something(5) If you want a reference to the function somewhere else you can do this: global alias = something print alias(i) On Tue, Jun 12, 2012 at 1:53 PM, Julio Sergio julioser...@gmail.com wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
On 6/12/2012 10:53 AM Julio Sergio said... snip 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, inmodule File tst.py, line 12, inmodule a = something(5) NameError: global name 'something' is not defined Do you have any comments? python executes each line as it encounters it. a=something(5) as you have it attempts to bind the label 'a' to the result of something(5) which has not yet been defined. You seem to want it to compile everything first, then execute but it doesn't work that way. Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
On 12/06/2012 18:53, Julio Sergio wrote: 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, inmodule File tst.py, line 11, inmodule 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, inmodule File tst.py, line 12, inmodule a = something(5) NameError: global name 'something' is not defined Do you have any comments? In Python, def is a statement, not a declaration. It binds the body of the function to the name when the def statement is run. A Python script is, basically, run from top to bottom, and both def and class are actually statements, not declarations. A function can refer to another function, even one that hasn't been defined yet, provided that it has been defined by the time it is called. For example, this: def first(): second() def second(): pass first() is OK because it defines function first, then function second, then calls first. By the time first calls second, second has been defined. -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
Jose H. Martinez josehmartinezz at gmail.com writes: You should define the function first and then call it. def something(i): return i a = something(5) If you want a reference to the function somewhere else you can do this: I know that. That was what I meant by changing the order of the definitions will work in my original message. And I insist in the issue, which is not trivial... In my message I mentioned crossed recursion, and I delve into it here: Suppose I have to define two functions, aa, and, bb that are designed to call each other: def aa(): ... ... a call of bb() somewhere in the body of aa ... def bb(): ... ... a call of aa() somewhere in the body of bb ... Whatever the order of definition of aa and bb the problem remains, one of the two identifiers is not known ... Most of computer languages have mechanisms to deal with this issue. Is there any in Python or it is in disadvantage with respect to other languages like C++, Java, Perl, PHP, etc.? -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
On Tue, Jun 12, 2012 at 2:33 PM, Julio Sergio julioser...@gmail.com wrote: Suppose I have to define two functions, aa, and, bb that are designed to call each other: def aa(): ... ... a call of bb() somewhere in the body of aa ... def bb(): ... ... a call of aa() somewhere in the body of bb ... Whatever the order of definition of aa and bb the problem remains, one of the two identifiers is not known ... This works just fine in python, exactly as you've written it. What's the actual problem you're having? -- Jerry -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
On 01/-10/-28163 01:59 PM, Julio Sergio wrote: 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. Mutual recursion isn't a problem: the following strange expression of factorial works fine: def strange_helper(x): return factorial(x) def factorial(x): if x==0: return 1 else: return x * strange_helper(x-1) print factorial(5) The reason is names are never looked up when the parser sees them, but rather only when execution reaches them. So the fact that 'factorial' hasn't been defined yet when the parser is dealing with 'strange_helper' is fine, because when 'strange_helper' is actually *called*, 'factorial' has been defined. Here's another example to illustrate, in a different manner that doesn't use this undefined thing: def foo(): print Inside the first version of foo def call_foo(): foo() call_foo() def foo(): print Inside the second version of foo call_foo() This prints: Inside the first version of foo Inside the second version of foo Evan -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
Julio Sergio wrote: Jose H. Martinez josehmartinezz at gmail.com writes: You should define the function first and then call it. def something(i): return i a = something(5) If you want a reference to the function somewhere else you can do this: I know that. That was what I meant by changing the order of the definitions will work in my original message. And I insist in the issue, which is not trivial... In my message I mentioned crossed recursion, and I delve into it here: Suppose I have to define two functions, aa, and, bb that are designed to call each other: def aa(): ... ... a call of bb() somewhere in the body of aa ... def bb(): ... ... a call of aa() somewhere in the body of bb ... Whatever the order of definition of aa and bb the problem remains No. The reply from MRAB explains this. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
Seems like what you need is from othermodule import bb def aa(): bb() On Tue, Jun 12, 2012 at 2:51 PM, Ethan Furman et...@stoneleaf.us wrote: Julio Sergio wrote: Jose H. Martinez josehmartinezz at gmail.com writes: You should define the function first and then call it. def something(i): return i a = something(5) If you want a reference to the function somewhere else you can do this: I know that. That was what I meant by changing the order of the definitions will work in my original message. And I insist in the issue, which is not trivial... In my message I mentioned crossed recursion, and I delve into it here: Suppose I have to define two functions, aa, and, bb that are designed to call each other: def aa(): ... ... a call of bb() somewhere in the body of aa ... def bb(): ... ... a call of aa() somewhere in the body of bb ... Whatever the order of definition of aa and bb the problem remains No. The reply from MRAB explains this. ~Ethan~ -- http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
Ethan Furman ethan at stoneleaf.us writes: No. The reply from MRAB explains this. ~Ethan~ Thanks, you're right! I was confusing statemens with declarations. -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
Julio Sergio wrote: Ethan Furman ethan at stoneleaf.us writes: No. The reply from MRAB explains this. ~Ethan~ Thanks, you're right! I was confusing statemens with declarations. Yeah, it took me a while to get that straight as well. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: using identifiers before they are defined
Julio Sergio julioser...@gmail.com writes: Suppose I have to define two functions, aa, and, bb that are designed to call each other: def aa(): ... ... a call of bb() somewhere in the body of aa ... def bb(): ... ... a call of aa() somewhere in the body of bb ... Whatever the order of definition of aa and bb the problem remains, one of the two identifiers is not known ... What problem? Can you show actual code that we can execute, which demonstrates the problem? -- \ “I turned to speak to God/About the world's despair; But to | `\ make bad matters worse/I found God wasn't there.” —Robert Frost | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list