繁体   English   中英

用于访问类变量的Python关键字

[英]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类,它可能是FooFoo的子类:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM