Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto __repr__ method

Tags:

python

I want to have simple representation of any class, like { property = value }, is there auto __repr__?

like image 210
Igor Golodnitsky Avatar asked Apr 15 '09 09:04

Igor Golodnitsky


People also ask

What is the __ repr __ method?

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 __ Repr__ in Python?

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.

What does def __ repr __( self do?

According to the official documentation, __repr__ is used to compute the “official” string representation of an object and is typically used for debugging.

What is __ repr __ Sqlalchemy?

The __repr__ function is defined by the designer of a type, in order to provide a means for users of the type to represent values of that type unambiguously, with a string.


1 Answers

Simplest way:

def __repr__(self):     return str(self.__dict__) 
like image 68
Shubham Chaudhary Avatar answered Oct 19 '22 06:10

Shubham Chaudhary