Python repr() Method
The repr()
method returns a string containing a printable representation of an object. The repr()
function calls the underlying __repr__()
function of the object.
Syntax:
repr(obj)
Parameters:
obj: Required. The object whose printable representation is to be returned.
Return Value:
Returns a string.
The following example demonstrates the repr()
method.
print(repr(10))
print(repr(10.5))
print(repr(True))
print(repr(4+2))
'10'
'10.5'
'True'
'6'
The repr()
function returns the string representation of the value passed to eval function by default. For the custom class object, it returns a string enclosed in angle brackets that contains the name and address of the object by default.
class student:
name=''
std = student()
repr(std)
'<main.student object at 0x0000000003B1FF98>'
Override the __repr__()
method to change this default behaviour, as shown below.
class student:
name=''
def __repr__(self):
return 'student class'
std = student()
repr(std)
student class