Chapter 14.3.5. Object Representation

Many object-oriented programming languages provide a facility to create a string representation of an object. In Java and JavaScript, for example, you can define a toString method to generate the string representation of an object. In Python, there are two different string methods: __zstr__ and __repr__. __str__ provides a string representation of the object that is suitable for users. _repr_ provides a representation for the programmers. The string returned by __repr__ ideally can be cut and pasted directly into the Python interpreter.

repr can prove very useful, especially when debugging. For this reason, MochiKit brings the repr function to JavaScript. Here’s an example:

 >>> animals = ["dog", "cat", ...

Get Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.