473,321 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Functions

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
Jul 18 '05 #1
3 1325
Thor <th******@yahoo.com> wrote in
news:bh************@ID-108351.news.uni-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 du****@rcp.co.uk
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?
Jul 18 '05 #2
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

Jul 18 '05 #3
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.