This web page provides an overview of class representation special methods in Python Object-Oriented Programming (OOP), focusing on the __str__() and __repr__() methods.
Abstract
The web page titled "Class Representation Special Methods: Python OOP Complete Course — Part 15" discusses the use of special methods in Python OOP to generate representative values for class objects. These methods include __str__() and __repr__(), which are used to create human-readable and machine-readable strings, respectively. The article provides examples of overriding these methods to customize the output of object representations. It also mentions that the article is part of a larger course on Python OOP and includes links to related resources.
Opinions
The author believes that class representation special methods are essential for generating representative values for class objects in Python OOP.
The author suggests that using the __str__() method can result in a more human-readable string representation of an object.
The author argues that the __repr__() method is useful for creating a machine-readable string representation of an object, which can be used to recreate the object.
The author encourages readers to subscribe to their posts and follow them on Medium.
The author promotes the benefits of Medium membership, including unlimited access to stories and earning potential.
The author provides links to additional resources, such as the complete course in Python OOP and a GitHub repository.
The author suggests that readers can use the provided links to navigate to the previous or next article in the course.
Class Representation Special Methods: Python OOP Complete Course — Part 15
Learn what are class representation special methods in Python OOP and how to override them.
They are the methods that are used to generate a representative value for your class objects.
The representative value could be:
A human-readable string of your object state, the values of instance attributes. For example, if you want to know some information about the object just by printing that object.
A machine-readable string to help regenerate the object itself.
A hash value for the object to give each object a unique ID.
2. Available Class Representation Special Methods
There are a lot of methods under this category. In this article, the two main special methods will be covered. These methods are:
__str__(): This method defines the behavior for when the
function str() is called with an object of your class.
__repr__(): This method defines the behavior for when the
function repr() is called with an object of your class.
To keep the article clean and concise, all of these methods’ definitions have been moved to this pdf file.
3. How to Override Class Representation Special Methods?
First, let us start talking about__str__()method
Assume that you have Student class which has the following instance attributes (id, first_name, last_name, full_name, age, and classess), and a student object. Let us print the result of str() method.
Output:
<__main__.Studentobject at 0x0000000004DCD6A0>
You got an ugly output that tells you the type of the object and in which memory location it is stored.
Now, let us override the __str__() method in order to get a human-readable string. Refer to the last method in the following code…
Output:
I am John Doe andmy age is25
Nice, right !!
Now let us talk about __repr__() method
Let us use the previous Student class and the previous student object. Try to print the result of repr() method.
Output:
<__main__.Studentobject at 0x0000000004DD7C88>
As you have seen, you got an ugly output again.
Now, let us override the __repr__() method in order to generate a machine-readable string. Refer to the last method in the following code…
Output:
Student("John", "Doe", 25)
In the previous example, you got a machine-readable string that you can use directly to redefine exactly the same Student object as follows:
Class Representation Special Methods are the methods that are used to generate a representative value for your class objects.
__str__(): is used usually to generate a human-readable string.
__repr__(): is used usually to generate a machine-readable string.
P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:
First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membershipwhichonly costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.