How to print actual name of variable class type in function?

前端 未结 4 1939
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 04:21

I\'m trying to return variable name, but i keep getting this:

Below is my cod

4条回答
  •  情书的邮戳
    2021-01-29 05:05

    __repr__ is the method that defines the name in your case. By default it gives you the object type information. If you want to print more apt name then you should override the __repr__ method

    Check below code for instance

    class class_with_overrided_repr:
        def __repr__(self):
            return "class_with_overrided_repr"
    
    class class_without_overrided_repr:
        pass
    
    x = class_with_overrided_repr()
    print x    # class_with_overrided_repr
    
    x = class_without_overrided_repr()
    print x    #  <__main__.class_without_overrided_repr instance at 0x7f06002aa368>
    

    Let me know if this what you want?

提交回复
热议问题