0

I'm trying to understand function environments (global, local). Specifically, I get very confused when there is a nested function that has already been defined globally, for example:

def g(x):
    print(x)

def f(f):
    f(1)

f(g)

Can someone please help me with this concept? I would greatly appreciate it.

Thanks.

Daenyth
  • 35,856
  • 13
  • 85
  • 124
bmacri
  • 9
  • 1
  • 1
  • 4
  • 4
    "I get very confused" By what? What is confusing? "help me with this concept"? Help you do what? Help you understand what? You need to be more clear on what part is confusing. When you run this at the `>>>` prompt, what happens? – S.Lott Feb 26 '12 at 05:49
  • 1
    "future questions"? Why wait? You can fix this question right now. It may help others who have similar confusion. It would help everyone to update the question right now to explain what's confusing. – S.Lott Feb 26 '12 at 18:40

3 Answers3

4

Python uses dictionaries to keep local and global variables in. When looking up a variable reference, it will look into the local dict first. If you want to reference a variable in the global dictionary, put the global keyword in front of it.

Also see answers to this question for more elaborate info.

Community
  • 1
  • 1
MvdD
  • 22,082
  • 8
  • 65
  • 93
0

I agree with user18044, however, is your confusion about the ’f(f)’? I agree that can be really confusing, especially in a non typed language. The argument to ’f’ is a function handle that has a local type scope with name ’f’. The way python decides which ’f’ gets used is explained by 18044. Python looks at the name ’f’ on the function definition and the local parameter ’f’ takes precidence over the global name ’f’ just like would be done if we had a global variable ’dude’ and a local variable ’dude’ in a function. The local overrides the global. Hopes this helps, and makes sense. :-)

macduff
  • 4,655
  • 18
  • 29
  • Yes, my confusion is about the f(f), exactly! It seems counter intuitive, just semantically, that local supersedes global. This does help, thank you very much. – bmacri Feb 26 '12 at 05:36
  • @Code_Phoenix: Please **update** the question to explain precisely what confuses you. Don't add import information as a comment on an answer. **Update** the question, please. – S.Lott Feb 26 '12 at 05:50
  • local superseding global is the normal behaviour in every language that has any concept of scope. That's what scope is for. It should take more effort to refer to something from "outside" than from something defined "locally", because the *exact reason* for defining things locally is that they are more likely to be useful locally (or rather, that they shouldn't be used elsewhere). – Karl Knechtel Feb 26 '12 at 08:49
0

The locals for a function consist of everything that was passed in, and every variable that is assigned to and not explicitly tagged as global (or nonlocal in 3.x).

The globals consist of everything that can be seen at global scope, including the function itself.

When a name is referenced, it is looked up in the locals first, and then in the globals if not found in the locals.

When the statement f(g) is run, the statement itself is at global scope, so there are no locals. f and g are both found in the globals: they are both functions. The function defined by def f... is called, with the function defined by def g... being passed as an argument.

When f(f) runs, f is in the locals for the function. It is bound to the passed-in value, which is the function defined by def g.... The body of the function has the statement f(1). 1 is a constant and no lookup is required. f is looked up in the locals, and the passed-in function is found. It - being the function known at global scope as g - is called.

Thus g is, likewise, run with the value 1 bound to the local variable x. That is forwarded to the function print (in 3.x; in 2.x, print is a keyword, so print x is a statement), which prints the value 1.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153