Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
using identifiers before they are defined
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Julio Sergio  
View profile  
 More options Jun 12 2012, 1:53 pm
Newsgroups: comp.lang.python
From: Julio Sergio <julioser...@gmail.com>
Date: Tue, 12 Jun 2012 17:53:31 +0000 (UTC)
Local: Tues, Jun 12 2012 1:53 pm
Subject: 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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Emile van Sebille  
View profile  
 More options Jun 12 2012, 2:17 pm
Newsgroups: comp.lang.python
From: Emile van Sebille <em...@fenx.com>
Date: Tue, 12 Jun 2012 11:17:16 -0700
Local: Tues, Jun 12 2012 2:17 pm
Subject: Re: using identifiers before they are defined
On 6/12/2012 10:53 AM Julio Sergio said...
<snip>

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Jun 12 2012, 2:25 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Tue, 12 Jun 2012 19:25:24 +0100
Local: Tues, Jun 12 2012 2:25 pm
Subject: Re: using identifiers before they are defined
On 12/06/2012 18:53, Julio Sergio wrote:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Julio Sergio  
View profile  
 More options Jun 12 2012, 2:33 pm
Newsgroups: comp.lang.python
From: Julio Sergio <julioser...@gmail.com>
Date: Tue, 12 Jun 2012 18:33:06 +0000 (UTC)
Local: Tues, Jun 12 2012 2:33 pm
Subject: 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.?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jerry Hill  
View profile  
 More options Jun 12 2012, 2:36 pm
Newsgroups: comp.lang.python
From: Jerry Hill <malaclyp...@gmail.com>
Date: Tue, 12 Jun 2012 14:36:41 -0400
Local: Tues, Jun 12 2012 2:36 pm
Subject: Re: using identifiers before they are defined

This works just fine in python, exactly as you've written it.  What's
the actual problem you're having?

--
Jerry


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Evan Driscoll  
View profile  
 More options Jun 12 2012, 2:33 pm
Newsgroups: comp.lang.python
From: Evan Driscoll <drisc...@cs.wisc.edu>
Date: Tue, 12 Jun 2012 13:33:31 -0500
Local: Tues, Jun 12 2012 2:33 pm
Subject: 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Furman  
View profile  
 More options Jun 12 2012, 2:51 pm
Newsgroups: comp.lang.python
From: Ethan Furman <et...@stoneleaf.us>
Date: Tue, 12 Jun 2012 11:51:56 -0700
Local: Tues, Jun 12 2012 2:51 pm
Subject: Re: using identifiers before they are defined

No.  The reply from MRAB explains this.

~Ethan~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Julio Sergio  
View profile  
 More options Jun 12 2012, 4:25 pm
Newsgroups: comp.lang.python
From: Julio Sergio <julioser...@gmail.com>
Date: Tue, 12 Jun 2012 20:25:57 +0000 (UTC)
Local: Tues, Jun 12 2012 4:25 pm
Subject: 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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ethan Furman  
View profile  
 More options Jun 12 2012, 4:46 pm
Newsgroups: comp.lang.python
From: Ethan Furman <et...@stoneleaf.us>
Date: Tue, 12 Jun 2012 13:46:08 -0700
Local: Tues, Jun 12 2012 4:46 pm
Subject: 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~


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options Jun 12 2012, 11:33 pm
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Wed, 13 Jun 2012 13:33:17 +1000
Local: Tues, Jun 12 2012 11:33 pm
Subject: Re: using identifiers before they are defined

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »