【发布时间】:2015-09-14 19:30:57
【问题描述】:
有人可以帮我处理 python 列表吗?我创建了一个全局变量和全局列表。更新了其他方法中的全局值。全局值更新正常,但全局列表给了我一个错误。
class Practice(object):
foo = []
var = 0;
def __updateVaribale(self):
global var
var = 9;
def __updateList(self):
global foo
foo.append("updateList 1")
def main(self):
self.__updateVaribale();
global var
print(var)
self.__updateList()
global foo
print(foo)
Obj = Practice();
Obj.main();
输出
9
Traceback (most recent call last):
File "Python Test\src\Practice.py", line 31, in <module>
Obj.main();
File "Python Test\src\Practice.py", line 26, in main
self.__updateList()
File "Python Test\src\Practice.py", line 18, in __updateList
foo.append("updateList 1")
NameError: name 'foo' is not defined
【问题讨论】:
-
这些是类变量。您可以使用
self访问它们,例如self.foo
标签: python list python-3.x