Python’s built-in repr(obj)
function returns the standard string representation of the provided object. This often includes the type and memory address of the object—for lack of further information. For example, the returned string representation may be '<main.Car object at 0x000001F66D11DBE0>'
for a custom object of type Car
. The function internally calls the method obj.__repr__()
which is defined per default for all objects.
Here’s an example:
>>> class Car: pass >>> repr(Car()) '<__main__.Car object at 0x000001F66D11DBE0>'
Syntax repr()
Syntax:
repr(object) # --> Returns standard (canonical) representation of an object.
Arguments | object | Object for which the string representation should be returned. |
Return Value | string | String representation of object. |
Video repr()
Usage Examples repr()
The following code shows you how to use the repr(x)
function on how to determine the string representation of some basic Python objects:
>>> repr(42) '42' >>> repr('42') "'42'" >>> repr([1, 2, 3]) '[1, 2, 3]' >>> repr({'Alice': 'Bob', 'Bob': 'Alice'}) "{'Alice': 'Bob', 'Bob': 'Alice'}" >>> repr(object) "<class 'object'>" >>> repr(repr) '<built-in function repr>'
Here’s how you can define the string representation of your own custom objects:
class Car: def __repr__(self): return 'black tesla' print(repr(Car())) # black tesla
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
How to Implement Your Own String Representation for a Custom Object
To implement your own string representation when using the repr(object)
function on your custom object
, overwrite the object.__repr__()
method when defining the class and return the desired string representation of the given object. Note that if there’s a __str__()
method defined, it takes precedence over the __repr__()
method that is only used as a fallback that’s implemented per default for any object.
class Car: def __init__(self, color, brand): self.color = color self.brand = brand def __str__(self): return 'Your car has color ' + self.color + ' and brand ' + self.brand def __repr__(self): return '123' porsche = Car('black', 'porsche') tesla = Car('silver', 'tesla') print(str(porsche)) print(str(tesla))
The output is:
Your car has color black and brand porsche Your car has color silver and brand tesla
Note how the __str__
method takes precedence over the __repr__
method. But if you skip the definition of the __str__
method, it’ll take the string returned by the __repr__
method:
class Car: def __init__(self, color, brand): self.color = color self.brand = brand def __repr__(self): return '123' porsche = Car('black', 'porsche') tesla = Car('silver', 'tesla') print(str(porsche)) print(str(tesla))
The output now is:
123 123
Summary
Python’s built-in repr(obj)
function returns the standard string representation of the provided object.
This often includes the type and memory address—for lack of further information about the object.
For example, the result may be '<main.Car object at 0x000001F66D11DBE0>'
for a custom object of type Car
.
>>> class Car: pass >>> repr(Car()) '<__main__.Car object at 0x000001F66D11DBE0>'
The function internally calls the method obj.__repr__()
which is defined per default for all objects.
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.