0
>>> a = 10
>>> def f(x):
  return x + a
>>> a = 3
>>> f(1)

According to my experience on Java, the definition of f contains a local variable a, but how could the global binding a been visible on the function f call stack environment?

I did a research on the python syntax and found that's true, could anybody offer some background on why python dealing variable scope this way? thanks.

Lawrence Liu
  • 454
  • 3
  • 18

6 Answers6

4

Your function call is in the last line. When the function gets called, python first looks up for local variables with name a, if not found, it goes into global scope, and in global scope, the last assigned value of a is 3 ( just before the function was called)

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • 2
    Actually Python will not look for a local variable `a`, locals are decided when the function is defined not when it is called, so it'll directly look for `a` in global namespace. – Ashwini Chaudhary Mar 11 '14 at 09:36
1

What you may find even stranger is that this will also work:

>>> def f(x):
  return x + a
>>> a = 3
>>> f(1)

Note that a hasn't even been defined before the function f. It still works because your call to f is after a is defined and placed in the global namespace. At that point, since f does not have a in its local namespace, it will fetch it from the global namespace.

You can fetch the contents of the global namespace and check for yourself with globals(), and the local namespace with locals(). There's also some neat tricks you can do by manipulating the namespaces directly, but that is in most cases considered bad coding practice in Python, unless you really have a compelling reason and know what you're doing.

George Bahij
  • 597
  • 2
  • 9
0

In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be a local. If a variable is ever assigned a new value inside the function, the variable is implicitly local, and you need to explicitly declare it as global.

Ref : http://effbot.org/pyfaq/what-are-the-rules-for-local-and-global-variables-in-python.htm

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Priyank Patel
  • 3,495
  • 20
  • 20
0

It would return 4 because you declare a and f(x) a function then you give valuea=3 and then you give x=1 so the function would return 3+1 which is 4

Naveed Yousaf
  • 133
  • 2
  • 13
0

Python decide variable scope in function based on where they have been assigned. As you didn't assigned variable 'a' inside function so it starts looking out and consider the global value.

DevC
  • 7,055
  • 9
  • 39
  • 58
-2

Java is a purely object-oriented language, while Python is not. Python supports both structural as well as object-oriented paradigms.

Global variables are part of the structural programming paradigm. So global variables will be available in the scope of a function, unless another variable with exactly the same name exists in the local scope of that function.

AndyG
  • 39,700
  • 8
  • 109
  • 143
Shreyas
  • 1,404
  • 16
  • 10