Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Functions

Reply
Thread Tools

Functions

 
 
Duncan Booth
Guest
Posts: n/a
 
      08-18-2003
Thor <(E-Mail Removed)> wrote in
news:bhqhm1$1vcq3$(E-Mail Removed)-berlin.de:

> In this hypothetical case:
>
> def f1:
> f3:
> def f2:
> def f3:
> pass
> f1:
> def f4:
> def f3:
> pass
> f1:
>
> would the function f1 execute the right f3 depending on from which
> functions is it called?


Why don't you load up the interactive interpreter and try running it?
You'll find a lot of mistakes in the hypothetical code you entered,
including the absence of argument lists after the function names, and the
spurious colons and lack of parenthese on the function calls.

I'll assume you actually meant something like:

def f1():
f3()
def f2():
def f3():
pass
f1()
def f4():
def f3():
pass
f1()

If the code above is indeed what you intended then calling either f2() or
f4() will result in a 'NameError' exception because there is no name 'f3'
in scope from inside f1(). There are local variables 'f3' inside both f2()
and f4(), but local variables are never visible nor accessible from outside
the functions in which they are defined. (They are visible from inside
nested functions, but that is not the situation here.)

To get code something like this to work, you should pass f3 as a parameter
to f1:

def f1(f3):
f3()
def f2():
def f3():
pass
f1(f3)
def f4():
def f3():
pass
f1(f3)

Remember, Python functions are just objects like any other and may be
assigned to variables or passed in and out of other functions.

--
Duncan Booth http://www.velocityreviews.com/forums/(E-Mail Removed)
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      08-18-2003
Thor wrote:

> In this hypothetical case:
>
> def f1:


I assume you mean def f1(): here and in similar cases (the
parentheses are syntactically mandatory).

> f3:


I assume you mean f3() here and in similar cases (the colon
would be a syntax error, the parentheses indicate a call is
being performed).

> def f2:
> def f3:
> pass
> f1:
> def f4:
> def f3:
> pass
> f1:
>
> would the function f1 execute the right f3 depending on from which
> functions is it called?


No. There is no "dynamic scoping" of names (and the rules are
exactly the same whether you're thinking of names of functions
or names of any other type of object). f1 would look up name
f3 in its LEXICAL scope, not find it, and therefore produce an
error. If you added
global f3
as the first statement of both f2 and f4, right before the
"def f3():" in each of them, then -- as it happens -- you would
get the behavior you're after, in this particular simple case
(both f2 and f4 would, with the 'global', clobber global name
f3 with their own version of function f3 on each execution --
and global names of this module which all functions share ARE
parts of the lexical scope searched for name resolution within
function f1).


Alex

 
Reply With Quote
 
 
 
 
Thor
Guest
Posts: n/a
 
      08-18-2003
In this hypothetical case:

def f1:
f3:
def f2:
def f3:
pass
f1:
def f4:
def f3:
pass
f1:

would the function f1 execute the right f3 depending on from which functions
is it called?
--
Thor -- Stockholm -- Sverige
 
Reply With Quote
 
Thor
Guest
Posts: n/a
 
      08-19-2003
Thanks to both. Of ourse the parentheses thin was worng, I was just trying
to make the most symplified code. I tt was just one possibility that arose
from the code I was doing.

--
Thor -- Stockholm -- Sverige
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
Passing member functions to C functions? Steven T. Hatton C++ 7 10-07-2004 02:12 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Help! Passing Templates functions to template functions ILLOGIC C++ 1 06-01-2004 10:51 PM
Exportable class functions as stand alone functions to .DLL or .SO Timothy Wong C++ 3 05-20-2004 01:44 PM



Advertisments