Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

The repr function

The reprt function is used to obtain a canonical string representation of the object. Backticks (also called conversion or reverse quotes) do the same thing. Note that you will have eval(repr(object)) == object most of the time.

>>> i = []
>>> i.append('item') >>> `i`
"['item']"
>>> repr(i)
"['item']"

Basically, the repr function or the backticks are used to obtain a printable representation of the object. you can control what your objects return for therepr function by defining the__repr__ method in your class.