Python Repr() with Examples
Here we are again back with a very different and useful function in python; repr().
And I am sure you don’t wanna miss it, after all, it’s like a practice assignment without which the code will be incomplete, and I bet you won’t regret using repr() in python, the more easy it is, the more frequently it is used and the more sorted it is.
Confused? Don’t worry we’ll learn this together. Since the fairy basket has so much more to offer, come let’s grab repr() out of it.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
What is Repr() in Python?
This string is intended to be the representation of the object, and suitable for display to the programmer, for instance, while working in interactive interpreter.
A repr() is used in storing the data of pythonic code in such a way that it can be extracted from the code with the library function.
A very basic code of repr is given below:
Python repr() Example:
class thisisrepr(): print("a conduct by repr as follow")
d = 8 #this will print d print(d)
Python repr() Parameters
The repr() function takes a single parameter only:
obj – the object whose structured printable representation has to be returned.
While the str() method produces human-readable string, we sometimes want the nitty-gritty details.
A repr() method is a correct check on the evaluation of a runtime code. It copies the value from the library and stores it in the function which is executable.
If a coder would like to produce a more useful result, he can override the repr() function.
import datetime dateandtime= datetime.datetime.now() print repr(dateandtime)
class TechVidvan solution: def __init__(first, second): abstract.firstl = first abstract.i= i def __repr__(first): return 'l(%s, %s)' % (abs.real, abs.imag) def __str__(first): return '%s + i%s' % (p.abs, p.abs) q=complex(1, 90) print str(q) print repr(q)
TechVidvan solution (1, 90)
Class Objects in Python
repr() method calls out the function from its internal libraries. Let’s find one of our own classes.
>>> class age : age='67' def __repr__(first): return repr(first.age) >>> o=age() >>> repr(o)
In this, we override the __repr__() method to get it to do what we want. However, we can’t overwrite in this function, as it will then not store the correct value as an input.
Difference Between str() and repr() in Python
- Str is used in creating a table of internal differences within the code. But at the same time repr() does it by merging the two codes from the same library.
- Str is used in the functions as a predefined library, but repr() does this by using the already existing library from the code.
x = str(input("WELCOME TO TECHVIDVAN")) repr(x)
import datetime now = datetime.datetime.now() str(now) '2020-08-09 01:30:45.200924' repr(now) >>> repr(now)
What makes repr() a good choice?
Repr is easy to learn and handy to use in the programs where the coder needs to debug the codes. It is an efficient function to extract information from the predefined libraries.
The function of repr() is well versed with the csv and data files so that it can work very well on the binary texts as well.
More than this repr() is understandable by the user, so basically the user knows what is the accurate function to perform on his code.
Now, as we come to the end of the article, python repr() functions seem to be easy, but for a good coder, nothing is difficult or easy until it’s practiced.
So go and start, try out yourself what is the difference between repr() and str().