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𤭢
which is not very helpful to others trying to understand your byte string. In the comments, John points out that in Windows, print(bob2)
results in something like bobð¤¢
. The problem is that Python detects the default encoding of your terminal/console and tries to decode the byte string according to that encoding. Since Ubuntu and Windows uses different default encodings (possibly utf-8
and cp1252
respectively), different results ensue.
In contrast, the repr of a string is unambiguous:
In [63]: print(repr(bob2))
'bob\xf0\xa4\xad\xa2'
When people post questions here on SO about Python strings, they are often asked to show the repr of the string so we know for sure what string they are dealing with.
In general, the repr should be an unambiguous string representation of the object. repr(obj)
calls the object obj
's __repr__
method. Since in your example the class A
does not have its own __repr__
method, repr(b)
resorts to indicating the class and memory address.
You can override the __repr__
method to give more relevant information.
In your example, '<__main__.A instance at 0x74d78>'
tells us two useful things:
- that
b
is an instance of class A
in the __main__
namespace,
- and that the object resides in
memory at address 0x74d78.
You might for instance, have two instances of class A
. If they have the same memory address then you'd know they are "pointing" to the same underlying object. (Note this information can also be obtained using id
).
bobð¤¢
... it should have 4 characters after 'bob' and I see 4 in IDLE but in this SO comment I see only 3; presumably \xAD is treated as a soft hyphen. (2) Useful for unicode strings, not just byte strings. Example:u'bob\U00024b62'
– Broglie