我已经经历了很多答案而没有找到我所理解的答案。这是一个基于我的真实程序中的问题的测试程序。我希望有一个我可以更改的类变量,并将更改应用于类的所有实例,但是对于类似的类,即使使用相同的表单也是如此。
很明显,我在第3行为X定义了一个类变量,在第9行为Y定义了一个类变量。我试图在第23-25行中访问这些。
我的模特是
#! /usr/bin/python -t
class X:
clsvar = "Animal"
def show(self):
clsvar
def chg(self,creature):
clsvar = creature
class Y:
clsvar = "Plant"
def show(self):
clsvar
def chg(self,creature):
clsvar = creature
class A(X):
pass
class B(X):
pass
class C(Y):
pass
a = A()
b = B()
c = C()
print "1 " + a.show()
print "2 " + b.show()
print "3 " + c.show()
a.chg( "Dog")
print "4 " + a.show()
print "5 " + b.show()
print "6 " + c.show()
我的结果是
Traceback (most recent call last):
File "180.py", line 23, in ?
print "1 " + a.show()
File "180.py", line 5, in show
clsvar
NameError: global name 'clsvar' is not defined
我原以为clsvar会出现在任何派生类中,而不需要是全局的。我显然在这里很愚蠢,但我已经尝试了几十种方法而没有成功。
顺便说一句,我能够在Ruby中做到这一点。
#! /usr/bin/ruby -w
class X
@@clsvar = "Animal"
def self.show
@@clsvar
end
def self.chg(creature)
@@clsvar = creature
end
end
class Y
@@clsvar = "Plant"
def self.show
@@clsvar
end
def self.chg(creature)
@@clsvar = creature
end
end
class A < X
A.show
end
class B < X
B.show
end
class C < Y
C.show
end
a = A
b = B
c = C
puts "1 " + a.show
puts "2 " + b.show
puts "3 " + c.show
a.chg( "Dog")
puts "4 " + a.show
puts "5 " + b.show
puts "6 " + c.show
输出是:
1 Animal
2 Animal
3 Plant
4 Dog
5 Dog
6 Plant
答案 0 :(得分:2)
要访问类变量,您必须执行此操作:
MyClass.clsvar
甚至这个:
an_instance.clsvar
后者只有在实例没有任何名为clsvar
的实例变量时才有效。(*)
Python不像Java。考虑到,与Java不同,Python 具有全局变量。例如:
a = 1
class MyClass:
a = 2
def show(self):
print(a)
show
方法会打印1
,因为它引用全局变量a
,而不是MyClass.a
。
(*)关于此的说明。您可以使用MyClass.var
访问self.var
,如果您不修改它就没问题。但设置值不等效。如果要设置类变量,则必须使用MyClass.var = value
而不是an_instance.var = value
。后者将创建一个名为var
的新实例变量,其值为value
,因此MyClass.var
仍将具有旧值。
顺便说一下,我不知道Ruby,但我认为@@
语法用于访问类变量,这就是它工作的原因。
最后,您的代码不正确。您可能希望在这些方法中添加一些return
语句,否则在执行时会得到一些TypeError
。