When is the output of repr useful?

2020-07-14 10:37发布

问题:

I have been reading about repr in Python. I was wondering what the application of the output of repr is. e.g.

class A:
 pass

repr(A) ='<class __main__.A at 0x6f570>'

b=A()
repr(b) = '<__main__.A instance at 0x74d78>'

When would one be interested in '<class __main__.A at 0x6f570>' or'<__main__.A instance at 0x74d78>'?

回答1:

Sometimes you have to deal with or present a byte string such as

bob2='bob\xf0\xa4\xad\xa2'

If you print this out (in Ubuntu) you get

In [62]: print(bob2)
bob              
                            
标签: python repr