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

Mailing List Archive: Python: Python

using identifiers before they are defined

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


juliosergio at gmail

Jun 12, 2012, 10:53 AM

Post #1 of 12 (885 views)
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


josehmartinezz at gmail

Jun 12, 2012, 11:16 AM

Post #2 of 12 (880 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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 <juliosergio [at] gmail> 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
>


emile at fenx

Jun 12, 2012, 11:17 AM

Post #3 of 12 (876 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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, in<module>
> File "tst.py", line 12, in<module>
> 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


python at mrabarnett

Jun 12, 2012, 11:25 AM

Post #4 of 12 (876 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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, 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?
>
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


juliosergio at gmail

Jun 12, 2012, 11:33 AM

Post #5 of 12 (875 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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


driscoll at cs

Jun 12, 2012, 11:33 AM

Post #6 of 12 (877 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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


malaclypse2 at gmail

Jun 12, 2012, 11:36 AM

Post #7 of 12 (877 views)
Permalink
Re: using identifiers before they are defined [In reply to]

On Tue, Jun 12, 2012 at 2:33 PM, Julio Sergio <juliosergio [at] gmail> 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


ethan at stoneleaf

Jun 12, 2012, 11:51 AM

Post #8 of 12 (876 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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


josehmartinezz at gmail

Jun 12, 2012, 12:02 PM

Post #9 of 12 (879 views)
Permalink
Re: using identifiers before they are defined [In reply to]

Seems like what you need is

from othermodule import bb

def aa():
bb()



On Tue, Jun 12, 2012 at 2:51 PM, Ethan Furman <ethan [at] stoneleaf> 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-list<http://mail.python.org/mailman/listinfo/python-list>
>


juliosergio at gmail

Jun 12, 2012, 1:25 PM

Post #10 of 12 (868 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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


ethan at stoneleaf

Jun 12, 2012, 1:46 PM

Post #11 of 12 (868 views)
Permalink
Re: using identifiers before they are defined [In reply to]

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


ben+python at benfinney

Jun 12, 2012, 8:33 PM

Post #12 of 12 (863 views)
Permalink
Re: using identifiers before they are defined [In reply to]

Julio Sergio <juliosergio [at] gmail> 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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


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