我是一个python初学者,用lutz的书来理解
classmethod
,
staticmethod
和
instancemethod
是的。此代码的目的是了解
cls
,请
self
,以及直接的类调用(
Spam1.numInstances
)通过计算创建的实例数。
这是从这本书中得到的一个例子。我不确定为什么是家长班(
Spam1
)属性(
numInstances
)调用时不递增
Sub1
和
Other1
,这是子类。
这是我的代码:
class Spam1:
numInstances = 0
def count(cls):
cls.numInstances += 1
print("In count -> number of instances: cls, Spam", cls.numInstances, Spam1.numInstances)
def __init__(self):
print("-----")
print("In init, before -> number of instances: self, Spam",self.numInstances,Spam1.numInstances )
self.count()
print("In init, after -> number of instances: self, Spam",self.numInstances,Spam1.numInstances )
print("-----")
count=classmethod(count)
class Sub1(Spam1):
numInstances = 0
class Other1(Spam1):
pass
a=Spam1() #Output after increment: 1,1,1 (self, cls, Spam1)
b=Spam1() #Output after increment: 2,2,2 (self, cls, Spam1)
c=Spam1() #Output after increment: 3,3,3 (self, cls, Spam1)
d=Sub1() #Output after increment: 1,1,3 (self, cls, Spam1)
e=Sub1() #Output after increment: 2,2,3 (self, cls, Spam1)
f=Other1() #Output after increment: 4,4,3 (self, cls, Spam1)
我花了一天时间试着调试这段代码,但我不明白
cls.numInstances
因为Pycharm会对
新民寺
处于调试模式。因为沮丧,我读了几篇这样的文章:
What does cls() function do inside a class method?
,
What is the 'cls' variable used for in Python classes?
和
Python - self, no self and cls
,但我不明白发生了什么。
具体来说,以下是我的问题:
a)为什么
温泉
当
d
,请
e
,和
f
是被创造出来的?
我试图回答这个问题:
我的理解是
CLS
用于访问类属性。为了
丁
和
E类
,
self.numInstances
用于访问实例属性,该属性为零,因为
第1款
将继承属性的值归零
努明斯坦
从
SPAM1号
是的。
CLS
访问的类属性
第1款
,这也与
第1款
上课。因此,
自己
和
CLS公司
我们在输出中看到的值是
第1款
实例和类。我的理解正确吗?
(第二章)
F型
继承
努明斯坦
从
SPAM1号
是的。因此,
努明斯坦赛尔夫
和
新民寺
属于
F型
从中获取值
SPAM1号
是的。它们的价值是递增的,但不是
SPAM1号
因为
CLS公司
指
其他1
因为
自己
指
F型
,这是
其他1
是的。因此,
SPAM1号
的
努明斯坦
从未被触碰过。
b)我是否理解
努明斯坦赛尔夫
我是说,
新民寺
,和
温泉
对的?如果没有,有人能解释一下吗?
我相信我的问题很基本。我希望有人能帮我。我迷路了。