Python repr built-in function

The Python repr built-in function returns a string of the object which are in printable form. This simply means, if any object is passed they are return in a string form.

If you pass a string object the repr function will return a string form of that object. Consider the code below.

>>> s1='New true'
>>> repr(s1)
"'New true'"
>>> s2="Double quotes string"
>>> repr( s2 )
"'Double quotes string'"
>>> repr( '''Text to you ''' )
"'Text to you '"
>>> print( repr(s1) )
'New true'

If you look at the ouput there is always an extra double quotes enclosing the single quoted string. And note if double or triple quotes are used they are replaced by the single quote, this is fine since all the quotations represent the same meaning.

The inside single quotes is the original quotes enclosing the string and the outside double quotes is added to represent the object in a string form.

If you pass an integer or a floating point value they are return as a string.

>>> #Passing integer
>>> repr( 12389 )
'12389'
>>> #Passing real value
>>> repr( 67.82920 )
'67.8292'
>>> from decimal import *
>>> #passing decimal value
>>> repr( Decimal(12.3))
"Decimal('12.300000000000000710542735760100185871124267578125')"

All the returned values are enclosed by single quotes to signify that it is a string type.

Similarly, passing any other type objects like list, tuple, etc. return the object in a string form.

 
>>> #Passing list
>>> repr( [23, 'text' , 9010] )
"[23, 'text', 9010]"
>>> #Passing set
>>> repr( {34, 78, 92 } )
'{34, 92, 78}'
>>> #Passind dictionary
>>> repr( {2:'4' , 3:'9' , 5:'25'} )
"{2: '4', 3: '9', 5: '25'}"


Passing class object to repr function

If we pass a class object to repr function, we can expect two types of output.
 
i) The output is a string representation of the object that contains the name of the type of the object and additional information including the name and address of the object may be present. This form of output is obtained if the class does not have the __repr__() method.
 
ii) If the class has the method __repr__() then calling repr will return whatever is returned by the __repr__() method.
 
Both the cases are discussed below.

Passing class object that does not include repr__() method

Let us define a class without the __repr__() method and observe the output of the repr function when the class object is passed.

>>class New:
	def __init__(new , n1):
		new.n1=n1

	
>>> repr( New )
'<__main__.New object at 0x0367B9D0>'

The output is a string that contains the name of the ‘class’ and the address of the object.


Pasing class having the __repr__() method

If the class has the __repr__() method then we can control the output of the repr function through __repr_().

(Do not mind the ‘…’ before the program code, it is part of the program when running the program in CMD prompt
Windows)

>>> class Broke:
...     def __init__(br , penny):
...             br.penny=penny
...     def __repr__(self):
...             return br.penny
...
>>> br=Broke("Variable names")
>>> repr(br)
'Variable names'

Since __repr__() returns the ‘penny’ stirng value the repr function also returns it.




Leave a Reply

Your email address will not be published. Required fields are marked *