>>> def using_func():
... print a
...
>>> using_func()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in using_func
NameError: global name 'a' is not defined
>>> def defining_func():
... global a
... a = 1
...
>>> using_func()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in using_func
NameError: global name 'a' is not defined
>>> defining_func()
>>> using_func()
1
首先,我完全同意其他关于避免全局变量的评论。你应该从重新设计开始避免它们。但要回答你的问题:
子例程的定义顺序无关紧要,调用它们的顺序无关紧要:
函数的编写顺序无关紧要。
xmlfile
的值将由调用函数的顺序决定。你知道吗但是,通常最好避免将值重新分配给函数内部的全局变量,因为这样会使分析函数的行为更加复杂。最好使用函数参数和/或返回值(或者使用类并使变量成为类属性):
相关问题 更多 >
编程相关推荐