89DEVs

Python repr() function

overview of repr()

The repr() function returns a printable representation of a given object. For many types the return value is a string that would result in the object with the same values if it is evaluated using the eval() function. For classes, the __repr__() method can be overridden to change the default behavior.

use of repr()

The repr() function can be used to yield a printable representation of the given object. Later the string can be turned into an object with the same values using the eval() function. # use of repr() and eval() class Dog: name = 'rex' rex = Dog() rex_printable = repr(rex) print(rex_printable) The object rex is turned into the printable representation. <__main__.Dog object at 0x10f2abe20>

syntax of repr()

The syntax of the repr() function is: repr(object)

arguments of repr()

The repr() function accepts exactly one argument, which is required. It is the object, whose printable representation has to be returned. If no argument or more than one argument is passed to the function, a TypeError exception is raised. TypeError: repr() takes exactly one argument (0 given)

return values of repr()

The repr() function returns a printable representation as a string of the object passed as the argument.

                
        

Summary


Click to jump to section