Functions

D

Duncan Booth

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.
 
A

Alex Martelli

Thor said:
In this hypothetical case:

def f1:

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

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
 
T

Thor

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

Thor

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top