class test(object):
class_var=5
def __init__(self):
self.abc=9
obj=test()
print("Instance Variable")
print obj.abc()
print("Class Variable")
print test.class_var()
print("Class variable through Instance Variable")
print obj.class_var()
test.class_var=99
print("After Changing")
print("Class Variable")
print test.class_var()
print("Class variable through Instance Variable")
print obj.class_var()
我是python的新手,我正在检查类变量和实例变量在python中的工作方式以及它们与java和c ++中的静态变量的相似之处。我尝试执行此代码,我收到此错误
F:\ pythont> python static.py实例变量跟踪(最新版本 最后呼叫):文件" static.py",第8行,in print obj.abc()TypeError:' int'对象不可调用
F:\ pythont>
我非常困惑,因为我很确定在不使用getter的情况下可以在python中访问这些变量。由于其他小程序在我直接访问时起作用:S
答案 0 :(得分:3)
正如错误消息所示,obj.abc
是一个整数。当你在它后面添加括号时,你试图调用它,但你不能调用整数。
要阅读变量值,只需使用obj.abc
,不带括号。