我浏览了很多答案,但没有找到我所理解的答案。这是一个基于我实际程序中的问题的测试程序。我想要一个可以更改的类变量,并将更改应用于类的所有实例,但不能应用于类似的类,即使具有相同的形式。
很明显,我在第 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
最佳答案
要访问类变量,您必须执行以下操作:
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
.
(*) 对此的说明。你可以使用self.var
访问MyClass.var
,如果你不修改它也可以。但设置该值不等效。如果要设置类变量,则必须使用 MyClass.var = value
而不是 an_instance.var = value
。后者将创建一个名为 var
的新实例变量,其值为 value
,因此 MyClass.var
仍将具有旧值。
顺便说一句,我不了解 Ruby,但我认为 @@
语法用于访问类变量,因此这就是它的工作原理。
最后,您的代码不正确。您可能希望在这些方法中放置一些 return
语句,否则在执行它时您会收到一些 TypeError
。
关于python从派生类访问实例中的类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12921356/