开发者

Simple Python variable scope

It seems to me that functions can reference variables outside of their scope but cannot set them. Is this correct? Am I understanding this right?

I also included the globals usage. I know they are bad ju-ju and will avoid them; I know how to get around t开发者_运维技巧his, but just wanted to be clear.

My example program:

import foo

# beginning of functions

# this one works because I look at the variable but dont modify it
def do_something_working:

    if flag_to_do_something:
          print "I did it"

# this one does not work because I modify the var
def do_something_not_working:

    if flag_to_do_something:
          print "I did it"
          flag_to_do_something = 0

# this one works, but if I do this God kills a kitten
def do_something_using_globals_working_bad_ju_ju:

    global flag_to_do_something

    if flag_to_do_something:
         print "I did it"
         flag_to_do_something = 0


# end of functions

flag_to_do_something = 1

do_something_working()
do_something_not_working()
do_something_using_globals_working_bad_ju_ju()


Correct. Well mostly. When you flag_to_do_something = 0 you are not modifying the variable, you are creating a new variable. The flag_to_do_something that is created in the function will be a separate link to (in this case) the same object. However, if you had used a function or operator that modified the variable in place, then the code would have worked.

Example:

g = [1,2,3]
def a():
    g = [1,2]
a()
print g #outputs [1,2,3]

g = [1,2,3]
def b():
    g.remove(3)
b()
print g #outputs [1,2]


Yep, pretty much. Note that "global" variables in Python are actually module-level variables - their scope is that Python module (a.k.a. source file), not the entire program, as would be the case with a true global variable. So Python globals aren't quite as bad as globals in, say, C. But it's still preferable to avoid them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜