[英]Python keyword to access class variable
考虑Python 3中的以下类:
class Foo(AnotherClass):
id_counter = 0
def __init__(self):
super().__init__()
self.id = Foo.id_counter
Foo.id_counter += 1
是否有一个关键字(在这种情况下类似于Python的super
)可用于访问类变量而不是放置类名Foo
?
type(self)
或self.__class__
将返回实际的self
类,它可能是Foo
或Foo
的子类:
class Foo(AnotherClass):
id_counter = 0
def __init__(self):
super().__init__()
self.id = type(self).id_counter
type(self).id_counter += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.