Something more beautiful than <__main__.MyClass instance at 0x1624710>

后端 未结 2 713
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-18 09:38

This is my class (as simple as it can be):

class MyClass():
    def __init__(self, id):
        self.id = id

    def __str__(self):
        return \"MyClass         


        
相关标签:
2条回答
  • 2021-01-18 09:49
    >>> class MyClass():
    ...     def __init__(self, id):
    ...         self.id = id
    ...     def __repr__(self):
    ...         return "MyClass #%d" % self.id
    ... 
    >>> c = MyClass(5)
    >>> c
    MyClass #5
    
    0 讨论(0)
  • 2021-01-18 10:06
    def __repr__(self):
        return 'MyClass #%d' % (self.id,)
    
    0 讨论(0)
提交回复
热议问题