Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python object.__repr__(self) should be an expression?

Tags:

python

I was looking at the builtin object methods in the Python documentation, and I was interested in the documentation for object.__repr__(self). Here's what it says:

Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines repr() but not str(), then repr() is also used when an “informal” string representation of instances of that class is required.

This is typically used for debugging, so it is important that the representation is information-rich and unambiguous

The most interesting part to me, was...

If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value

... but I'm not sure exactly what this means. It says it should look like an expression which can be used to recreate the object, but does that mean it should just be an example of the sort of expression you could use, or should it be an actual expression, that can be executed (eval etc..) to recreate the object? Or... should it be just a rehasing of the actual expression which was used, for pure information purposes?

In general I'm a bit confused as to exactly what I should be putting here.

like image 700
Alex McBride Avatar asked Jan 16 '09 22:01

Alex McBride


People also ask

What is __ repr __( self?

__repr__ (self) Returns a string as a representation of the object. Ideally, the representation should be information-rich and could be used to recreate an object with the same value.

What does __ repr __ mean in Python?

The __repr__ method returns the string representation of an object. Typically, the __repr__() returns a string that can be executed and yield the same value as the object. In other words, if you pass the returned string of the object_name.

What is the purpose of __ repr __?

Python __repr__() function returns the object representation in string format. This method is called when repr() function is invoked on the object. If possible, the string returned should be a valid Python expression that can be used to reconstruct the object again.

How do you represent a string representation of an object in Python?

You can use repr() for python-statement representation of the object, str() for ascii string representation, and unicode() for Unicode string representation.


2 Answers

>>> from datetime import date >>> >>> repr(date.today())        # calls date.today().__repr__() 'datetime.date(2009, 1, 16)' >>> eval(_)                   # _ is the output of the last command datetime.date(2009, 1, 16) 

The output is a string that can be parsed by the python interpreter and results in an equal object.

If that's not possible, it should return a string in the form of <...some useful description...>.

like image 51
Georg Schölly Avatar answered Oct 01 '22 02:10

Georg Schölly


It should be a Python expression that, when eval'd, creates an object with the exact same properties as this one. For example, if you have a Fraction class that contains two integers, a numerator and denominator, your __repr__() method would look like this:

# in the definition of Fraction class def __repr__(self):     return "Fraction(%d, %d)" % (self.numerator, self.denominator) 

Assuming that the constructor takes those two values.

like image 37
Paige Ruten Avatar answered Oct 01 '22 03:10

Paige Ruten