Skip to main content
 首页 » 编程设计

python之如何在python中引用类本身

2025年05月04日9wuhuacong

<分区>

我想在 python 中有这样的简单类常量:

class FooBarBaz: 
    BAR = 123 
 
    @staticmethod 
    def getBar(): 
        return BAR # this would not work, of course 
 
        return FooBarBaz.BAR # this would work but is "way too long" 

有没有更短的方法从方法内部引用类本身,而不是当前实例?它不仅适用于静态方法,而且通常适用于 __class__ 关键字或其他内容。