Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Variables are still accessible if defined in try or if?

Tags:

python

scope

I'm a Python beginner and I am from C/C++ background. I'm using Python 2.7.

I read this article: A Beginner’s Guide to Python’s Namespaces, Scope Resolution, and the LEGB Rule, and I think I have some understanding of Python's these technologies.

Today I realized that I can write Python code like this:

if condition_1:
    var_x = some_value
else:
    var_x = another_value
print var_x

That is, var_x is still accessible even it is not define before the if. Because I am from C/C++ background, this is something new to me, as in C/C++, var_x are defined in the scope enclosed by if and else, therefore you cannot access it any more unless you define var_x before if.

I've tried to search the answers on Google but because I'm still new to Python, I don't even know where to start and what keywords I should use.

My guess is that, in Python, if does not create new scope. All the variables that are newly defined in if are just in the scope that if resides in and this is why the variable is still accessible after the if. However, if var_x, in the example above, is only defined in if but not in else, a warning will be issued to say that the print var_x may reference to a variable that may not be defined.

I have some confidence in my own understanding. However, could somebody help correct me if I'm wrong somewhere, or give me a link of the document that discusses about this??

Thanks.

like image 410
yaobin Avatar asked Jul 19 '15 19:07

yaobin


1 Answers

My guess is that, in Python, if does not create new scope. All the variables that are newly defined in if are just in the scope that if resides in and this is why the variable is still accessible after the if.

That is correct. In Python, namespaces, that essentially decide about the variable scopes, are only created for modules, and functions (including methods; basically any def). So everything that happens within a function (and not in a sub-function) is placed in the same namespace.

It’s important to know however that the mere existance of an assignment within a function will reserve a name in the local namespace. This makes for some interesting situations:

def outer ():
    x = 5
    def inner ():
        print(x)
        # x = 10
    inner()
outer()

In the code above, with that line commented out, the code will print 5 as you may expect. That’s because inner will look in the outer scope for the name x. If you add the line x = 10 though, the name x will be local to inner, so the earlier look up to x will look in the local namespace of inner. But since it hasn’t been assigned yet, you will receive an UnboundLocalError (“local variable 'x' referenced before assignment”). The nonlocal statement was added in Python 3 to overcome one issue from this: The situation where you want to actually modify the x of the outer scope within the inner function.

For more information on name lookup, see this related question.

like image 95
poke Avatar answered Sep 21 '22 03:09

poke