是否有可能间接访问python中的当前类名?

这个问题在这里已经有了答案:

  • Python - 从实例2的答案访问类变量

  • 你可以在python中使用self关键字而不是使用当前的类名。

    class MyLongAdvancedClassName:
        ...
    
        _template = " My long advanced text %i "
        def method(self):
            print self._template
    

    希望能帮助到你。


    可能你正在寻找的是使用自我(看San的答案)。 但是这需要你有一个类的实例。 如果你想用类自己做,那么你可以使用classmethod装饰器,像这样:

    class MyLongAdvancedClassName:
        ...
    
        _template = " My long advanced text %i "
        @classmethod
        def method(cls):
            print cls._template
    
    链接地址: http://www.djcxy.com/p/40951.html

    上一篇: Is it possible to indirectly access current class name in python?

    下一篇: method to print name of an instance of a class