Is it possible to indirectly access current class name in python?

This question already has an answer here:

  • Python - Access class variable from instance 2 answers

  • You can make use of self keyword in python instead of using the current class name.

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

    Hope it helps.


    Probably what you are looking for is to use self (look at San's answer). However this requires you to have an instance of the class. If you want to do it with the class itself then you can use the classmethod decorator like this:

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

    上一篇: 从实例获取模型名称

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