How do I change the string representation of a Python class?

How do I change the string representation of a Python class?

The closest equivalent to Javas toString is to implement __str__ for your class. Put this in your class definition:

def __str__(self):
     return foo

You may also want to implement __repr__ to aid in debugging.

See here for more information:

This is not as easy as it seems, some core library functions dont work when only str is overwritten (checked with Python 2.7), see this thread for examples
How to make a class JSON serializable
Also, try this

import json

class A(unicode):
    def __str__(self):
        return a
    def __unicode__(self):
        return ua
    def __repr__(self):
        return a

a = A()
json.dumps(a)

produces


and not

a

as would be expected.

EDIT: answering mchicagos comment:

unicode does not have any attributes — it is an immutable string, the value of which is hidden and not available from high-level Python code. The json module uses re for generating the string representation which seems to have access to this internal attribute. Heres a simple example to justify this:

b = A(b)
print b

produces

a

while

json.dumps({b: b})

produces

{b: b}

so you see that the internal representation is used by some native libraries, probably for performance reasons.

See also this for more details: http://www.laurentluce.com/posts/python-string-objects-implementation/

How do I change the string representation of a Python class?

Leave a Reply

Your email address will not be published. Required fields are marked *