Skip to main content
 首页 » 编程设计

python之滚动我自己的 __repr__

2025年05月04日14birdshome

我想自己写__repr__对于我定义的某些类。我希望它类似于默认的 <__main__.O object at 0x00D229D0> ,除了那里有一些其他细节。我如何重现 <__main__.O object at 0x00D229D0>东西?

请您参考如下方法:

参见 http://docs.python.org/reference/datamodel.html#object.repr

#!/usr/bin/env python 
class O(object): 
    def __repr__(self): 
        return '<%s.%s object at 0x%x>'%(self.__module__,self.__class__.__name__,id(self)) 
o=O() 
print(repr(o)) 
 
# <__main__.O object at 0xb7e7d0cc>