2

I have my unique class I created and i wish to control the output of some flow..e.g:

obj = MyObject()
print obj

so instead of what python does, I want to alter the output. how do I do such a thing?

4

2 回答 2

7

改变__str__方法

class MyObject(object):
    def __str__(self):
        return 'foo'


>>> print MyObject()
foo
于 2013-05-18T13:31:26.183 回答
7

您可以覆盖__str____repr__

于 2013-05-18T13:31:37.213 回答