Scope in Functions vs. Methods [duplicate] - python

This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 8 years ago.
I am wondering why a method of a class doesn't look into its enclosing scope, if a name is not defined.
def test_scope_function():
var = 5
def print_var():
print(var) # finds var from __test_scope_function__
print_var()
globalvar = 5
class TestScopeGlobal:
var = globalvar # finds globalvar from __main__
#staticmethod
def print_var():
print(TestScopeGlobal.var)
class TestScopeClass():
var = 5
#staticmethod
def print_var():
print(var) # Not finding var, raises NameError
test_scope_function()
TestScopeGlobal.print_var()
TestScopeClass.print_var()
I would expect TestScopeClass.print_var() to print 5 since it can read classvar in the TestScopeClass body.
Why this behavior? And what should i read in the docs to get to know about it.

Scopes are searched as follows:
the innermost scope, which is searched first, contains the local names
the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
the next-to-last scope contains the current module’s global names
the outermost scope (searched last) is the namespace containing built-in names
(emphasis added). Enclosing classes are not searched, because they are not listed. This behavior is deliberate, because otherwise the descriptor protocol, which among other things provides the self argument to methods, would not have a chance to fire.

Per the Python documentation (emphasis mine):
The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods

Related

Python's free variables behaviour with inner classes [duplicate]

Consider the following snippet of python code:
x = 1
class Foo:
x = 2
def foo():
x = 3
class Foo:
print(x) # prints 3
Foo.foo()
As expected, this prints 3.
But, if we add a single line to the above snippet, the behavior changes:
x = 1
class Foo:
x = 2
def foo():
x = 3
class Foo:
x += 10
print(x) # prints 11
Foo.foo()
And, if we switch the order of the two lines in the above example, the result changes yet again:
x = 1
class Foo:
x = 2
def foo():
x = 3
class Foo:
print(x) # prints 1
x += 10
Foo.foo()
I'd like to understand why this occurs, and more generally, understand the scoping rules that cause this behavior. From the LEGB scoping rule, I would expect that both snippets print 3, 13, and 3, since there is an x defined in the enclosing function foo().
Class block scope is special. It is documented here:
A class definition is an executable statement that may use and define
names. These references follow the normal rules for name resolution
with an exception that unbound local variables are looked up in the
global namespace. The namespace of the class definition becomes the
attribute dictionary of the class. The scope of names defined in a
class block is limited to the class block; it does not extend to the
code blocks of methods – this includes comprehensions and generator
expressions since they are implemented using a function scope.
Basically, class blocks do not "participate" in creating/using enclosing scopes.
So, it is actually the first example that isn't working as documented. I think this is an actual bug.
EDIT:
OK, so actually, here's some more relevant documentation from the data model, I think it all is actually consistent with the documentation:
The class body is executed (approximately) as exec(body, globals(),
namespace). The key difference from a normal call to exec() is that
lexical scoping allows the class body (including any methods) to
reference names from the current and outer scopes when the class
definition occurs inside a function.
So class blocks do participate in using enclosing scopes, but for free variables (as is normal anyway). In the first piece of documentation that I'm quoting, the part about "unbound local variables are looked up in the global namespace" applies to variables that would normally be marked local by the complier. So, consider this notorious error, for example:
x = 1
def foo():
x += 1
print(x)
foo()
Would throw an unbound local error, but an equivalent class definition:
x = 1
class Foo:
x += 1
print(x)
will print 2.
Basically, if there is an assignment statement anywhere in a class block, it is "local", but it will check in the global scope if there is an unbound local instead of throwing the UnboundLocal error.
Hence, in your first example, it isn't a local variable, it is simply a free variable, and the resolution goes through the normal rules. In your next two examples, you us an assignment statemnt, marking x as "local", and thus, it will be looked up in the global namespace in case it is unbound in the local one.

Global variable used inside a instance method without "global" [duplicate]

This question already has answers here:
Why isn't the 'global' keyword needed to access a global variable?
(11 answers)
Closed 7 years ago.
I thought global variables inside a Python function had to be declared global. So why does the following compile and run without error?
#!/usr/bin/env python
text = "why is this seen?"
class Foo:
def doit(self):
print(text)
x = Foo()
x.doit()
I'd appreciate a citation to the Python3 manual if possible.
You asked for a reference to the Python 3 manual. I've bolded the section that says you don't need to use the global keyword to reference free variables.
https://docs.python.org/3/reference/simple_stmts.html?highlight=global#grammar-token-global_stmt
7.12. The global statement
global_stmt ::= "global" identifier ("," identifier)*
The global statement is a declaration which holds for the
entire current code block. It means that the listed identifiers are to
be interpreted as globals. It would be impossible to assign to a
global variable without global, although free variables may refer to
globals without being declared global.
Note that in most code all of the classes and functions that you reference are globals (or builtins) but you didn't think twice about not needing global print before calling it.
Its all about scope, since text is declared outside, free from any class or function, it can be reached from anywhere. To get a better idea, consider these two examples:
#!/usr/bin/env python
text = "why is this seen?"
class Foo:
def doit(self):
text = "this is changed"
print(text)
x = Foo()
x.doit()
print text
In the above example, we overwrite the text variable locally, in the Foo, class, but the global instance of text is the same. But in this instance:
#!/usr/bin/env python
text = "why is this seen?"
class Foo:
def doit(self):
global text
text = "this is changed"
print(text)
x = Foo()
x.doit()
print text
We declare that we want the global version of text and then we can modify it.
BUT: global variables are frowned upon, consider using input arguments to functions and returning new values instead of having variable globally accessible everywhere
The right way to do it:
#!/usr/bin/env python
class Foo:
text = "why is this seen?"
def doit(self):
print(self.text)
x = Foo()
x.doit()
Have text encapsulated in the class!
You don't need to specify a variable using global if you just need to access it. You can do that without a global.
Here, Python will look in the class Foo scope first for the text variable. Since, it does not find the variable text in the Foo class so it will look into outer scope. Now, it finds the variable text, so it uses that value to print the output.
According to Python docs, at any time during execution, there are at least three nested scopes whose namespaces are directly accessible:
the innermost scope, which is searched first, contains the local names
the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also
non-global names
the next-to-last scope contains the current module’s global names
the outermost scope (searched last) is the namespace containing built-in names

Why doesn't Python's nonlocal keyword like the global scope?

In Python 3.3.1, this works:
i = 76
def A():
global i
i += 10
print(i) # 76
A()
print(i) # 86
This also works:
def enclosing_function():
i = 76
def A():
nonlocal i
i += 10
print(i) # 76
A()
print(i) # 86
enclosing_function()
But this doesn't work:
i = 76
def A():
nonlocal i # "SyntaxError: no binding for nonlocal 'i' found"
i += 10
print(i)
A()
print(i)
The documentation for the nonlocal keyword states (emphasis added):
The nonlocal statement causes the listed identifiers to refer to
previously bound variables in the nearest enclosing scope.
In the third example, the "nearest enclosing scope" just happens to be the global scope. So why doesn't it work?
PLEASE READ THIS BIT
I do notice that the documentation goes on to state (emphasis added):
The [nonlocal] statement allows encapsulated code to
rebind variables outside of the local scope besides the global
(module) scope.
but, strictly speaking, this doesn't mean that what I'm doing in the third example shouldn't work.
The search order for names is LEGB, i.e Local, Enclosing, Global, Builtin. So the global scope is not an enclosing scope.
EDIT
From the docs:
The nonlocal statement causes the listed identifiers to refer to
previously bound variables in the nearest enclosing scope. This is
important because the default behavior for binding is to search the
local namespace first. The statement allows encapsulated code to
rebind variables outside of the local scope besides the global
(module) scope.
why is a module's scope considered global and not an enclosing one? It's still not global to other modules (well, unless you do from module import *), is it?
If you put some name into module's namespace; it is visible in any module that uses module i.e., it is global for the whole Python process.
In general, your application should use as few mutable globals as possible. See Why globals are bad?:
Non-locality
No Access Control or Constraint Checking
Implicit coupling
Concurrency issues
Namespace pollution
Testing and Confinement
Therefore It would be bad if nonlocal allowed to create globals by accident. If you want to modify a global variable; you could use global keyword directly.
global is the most destructive: may affect all uses of the module anywhere in the program
nonlocal is less destructive: limited by the outer() function scope (the binding is checked at compile time)
no declaration (local variable) is the least destructive option: limited by inner() function scope
You can read about history and motivation behind nonlocal in PEP: 3104
Access to Names in Outer Scopes.
It depends upon the Boundary cases:
nonlocals come with some senstivity areas which we need to be aware of. First, unlike the global statement, nonlocal names really must have previous been assigned in an enclosing def's scope when a nonlocal is evaluated or else you'll get an error-you cannot create them dynamically by assigning them anew in the enclosing scope. In fact, they are checked at function definition time before either or nested function is called
>>>def tester(start):
def nested(label):
nonlocal state #nonlocals must already exist in enclosing def!
state = 0
print(label, state)
return nested
SyntaxError: no binding for nonlocal 'state' found
>>>def tester(start):
def nested(label):
global state #Globals dont have to exits yet when declared
state = 0 #This creates the name in the module now
print(label, state)
return nested
>>> F = tester(0)
>>> F('abc')
abc 0
>>> state
0
Second, nonlocal restricts the scope lookup to just enclosing defs; nonlocals are not looked up in the enclosing module's global scope or the built-in scope outside all def's, even if they are already there:
for example:-
>>>spam = 99
>>>def tester():
def nested():
nonlocal spam #Must be in a def, not the module!
print('current=', spam)
spam += 1
return nested
SyntaxError: no binding for nonlocal 'spam' found
These restrictions make sense once you realize that python would not otherwise generally know enclosing scope to create a brand-new name in. In the prior listing, should spam be assigned in tester, or the module outside? Because this is ambiguous, Python must resolve nonlocals at function creation time, not function call time.
The answer is that the global scope does not enclose anything - it is global to everything. Use the global keyword in such a case.
Historical reasons
In 2.x, nonlocal didn't exist yet. It wasn't considered necessary to be able to modify enclosing, non-global scopes; the global scope was seen as a special case. After all, the concept of a "global variable" is a lot easier to explain than lexical closures.
The global scope works differently
Because functions are objects, and in particular because a nested function could be returned from its enclosing function (producing an object that persists after the call to the enclosing function), Python needs to implement lookup into enclosing scopes differently from lookup into either local or global scopes. Specifically, in the reference implementation of 3.x, Python will attach a __closure__ attribute to the inner function, which is a tuple of cell instances that work like references (in the C++ sense) to the closed-over variables. (These are also references in the reference-counting garbage-collection sense; they keep the call frame data alive so that it can be accessed after the enclosing function returns.)
By contrast, global lookup works by doing a chained dictionary lookup: there's a dictionary that implements the global scope, and if that fails, a separate dictionary for the builtin scope is checked. (Of course, writing a global only writes to the global dict, not the builtin dict; there is no builtin keyword.)
Theoretically, of course, there's no reason why the implementation of nonlocal couldn't fall back on a lookup in the global (and then builtin) scope, in the same way that a lookup in the global scope falls back to builtins. Stack Overflow is not the right place to speculate on the reason behind the design decision. I can't find anything relevant in the PEP, so it may simply not have been considered.
The best I can offer is: like with local variable lookup, nonlocal lookup works by determining at compile time what the scope of the variable will be. If you consider builtins as simply pre-defined, shadow-able globals (i.e. the only real difference between the actual implementation and just dumping them into the global scope ahead of time, is that you can recover access to the builtin with del), then so does global lookup. As they say, "simple is better than complex" and "special cases aren't special enough to break the rules"; so, no fallback behaviour.

Accessing module level variables, from within a function in the module [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 8 months ago.
I'd like to be able to do something like this:
#mymodule
var = None
def load():
var = something()
Other module(s):
#secondmodule
import mymodule
mymodule.load()
#thirdmodule
from mymodule import var
print var #Shouldn't be None
But I don't know how to reference a modules variable from a function defined in the module.
Is this possible? Or am I going to need to put a global declaration in every place I wan't to use this.
Or am I going at this completely wrong?
Just change the function definition to:
def load():
global var # this line has been added to the original code
var = something()
Global variables are read-only from sibling methods. More accurately unless a variable is specified as global, Python consider it as local, but a read access to a local variable name will reach module-level scope if the name is not present in local scope.
See also use of “global” keyword in python and the doc for more details about the global statement
You seem to mostly have it. You are missing only the fact that "module-level" variables are called global in Python. (They are not truly global, but only global to the module they are declared in, in other words.)
In any function where you modify a global variable (you want to make the name refer to a different object), it must be declared global. So your load() function needs a global var at the beginning. If you are only using the value of a global variable, or if it is a mutable type such as a list and you are modifying it, but not changing the object that the name points to, you needn't declare it global.
The import statement is, as you have discovered, how you can import a module-level variable from one module into another.

Local functions in Python [duplicate]

This question already has answers here:
Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?
(9 answers)
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 6 months ago.
In the following Python code, I get an UnboundLocalError. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I recognise that a is an immutable value in this context, but that should not be a problem.
def outer():
a = 0
def inner():
a += 1
inner()
outer()
It would seem that the inner function has received copies of all the references in the parent function, as I do not get the UnboundLocalError exception if the value of a is wrapped in a mutable type.
Is someone able to clarify the behaviour here, and point me to the appropriate Python documentation on this?
I believe you're correct in seeing this as a "mutability" problem. While the code you posted does throw an "UnboundLocalError", the following code does not:
def outer():
a = 0
def inner():
print a
inner()
outer()
Python doesn't allow you to reassign the value of a variable from an outer scope in an inner scope (unless you're using the keyword "global", which doesn't apply in this case).
Check out the bottom section of the "classes" documentation in this Python 2.6.2 documentation:
9.2. Python Scopes and Namespaces
[…] If a name is declared global, then all references and assignments go
directly to the middle scope containing the module’s global names.
Otherwise, all variables found outside of the innermost scope are
read-only (an attempt to write to such a variable will simply create a
new local variable in the innermost scope, leaving the identically
named outer variable unchanged).
Your "UnboundLocalError" is because your function is actually declaring a new variable called "a" and then immediately trying to do a "+=" operation on it, but this fails because "a" does not have a value yet. (View the "a+=1" as "a = a+1" and you can see the problem if "a" is undefined).
In general, if you're going to want to modify "a", the way people usually get around it is to use a mutable type to pass "a" around (such as a list or a dictionary). You can modify "a" via the contents of the mutable type (as you probably noticed in your testing with this setup).
Hope that helps!
You should specify your variable as nonlocal to preserve it's state in closure, so definition should be like this
def outer():
a = 0
def inner():
nonlocal a
a += 1
inner()
Try binding the variable as an argument.
def outer():
a = 0
def inner(a=a):
a += 1
inner()
outer()
I'll try and dig up the appropriate documents.
edit
Since you want the inner function to have a side effect on the outer scope, then you need to use a mutable datatype like a list. Integers and strings are immutable.
def outer():
a = [0]
def inner():
a[0] += 1
inner()
print a[0]
outer()

Categories