Python function global variables?

前端 未结 6 814
广开言路
广开言路 2020-11-22 04:49

I know I should avoid using global variables in the first place due to confusion like this, but if I were to use them, is the following a valid way to go about using them? (

6条回答
  •  执笔经年
    2020-11-22 05:14

    If you want to simply access a global variable you just use its name. However to change its value you need to use the global keyword.

    E.g.

    global someVar
    someVar = 55
    

    This would change the value of the global variable to 55. Otherwise it would just assign 55 to a local variable.

    The order of function definition listings doesn't matter (assuming they don't refer to each other in some way), the order they are called does.

提交回复
热议问题