Mastering String Formatting for Custom Objects in Python
Written on
Chapter 1: Introduction to Python's Special Methods
In Python, underscores play a significant role in attribute names, especially when they appear as pairs of double underscores surrounding an attribute. These are referred to as special or magic methods.
One of the first special methods many programmers encounter is the initialization method, __init__, used to create instances of Python objects. Here’s a simple example:
class Student:
def __init__(self, name):
self.name = name
When you create an instance of this class, you can examine it by entering the object variable in an interactive console:
>>> student0 = Student("John Smith")
>>> student0
<__main__.Student at 0x11883aa60>
However, the output is rather uninformative, only displaying the class type and memory address. To make object representations more meaningful, we can utilize string formatting methods for custom classes. This article will explore three essential special methods.
Section 1.1: The __repr__ Method
The __repr__ method defines how an object is represented as a string. As demonstrated earlier, when a Python object is printed in an interactive console, it outputs its representation. Here’s how you can implement it:
It’s important to ensure that the __repr__ method returns a string. If a non-string is returned, a TypeError will occur. The ideal string returned should enable users to reconstruct the object, allowing them to create a new object with identical attributes.
With an updated __repr__ method, you can now inspect the object:
>>> student0 = Student("John Smith")
>>> student0
Student('John Smith')
You can also use the built-in repr() function to obtain the representation string. Users might expect that this string can be evaluated to create another object using eval():
>>> student1 = eval(repr(student0))
>>> student1
Student('John Smith')
Additionally, when using f-strings, you can include the object's attribute and specify !r to denote that the raw representation should be used. Without !r, the f-string will call the __str__ method instead.
>>> name = "John Smith"
>>> print(f"Student({name!r})")
Student('John Smith')
>>> print(f"Student({name})")
Student(John Smith)
In some instances, a meaningful string for reconstruction might not be feasible. In such cases, it’s common to enclose the class name and a brief summary in angle brackets. The following code illustrates this with built-in classes.
Section 1.2: The __str__ Method
Another crucial special method for formatting is the __str__ method. Below is an example of how to override it within a custom class:
With the modified class, observe when this method is triggered:
>>> print(student0)
__str__ is called
>>> str(student0)
'__str__ is called'
>>> f"{student0}"
'__str__ is called'
The __str__ method can be invoked in multiple scenarios: using the print() function, the str() function, or within an f-string.
While the usage of __str__ is clear, the best practices for defining its output may vary. Generally, the output should provide a descriptive summary of the instance. If __str__ is not defined, Python will revert to calling __repr__.
Section 1.3: The __format__ Method
The __format__ method is another vital special method that allows for customized string formatting. Below is its signature in a custom class:
As previously noted, both f-strings and the built-in format() function can invoke the __format__ method. Here's how it behaves:
>>> student0 = Student("John Smith")
>>> f"{student0}"
'__format__ is called'
>>> format(student0)
'__format__ is called'
>>> print(student0)
<__main__.Student object at 0x114a92c10>
>>> str(student0)
'<__main__.Student object at 0x114a92c10>'
Unlike __repr__ and __str__, the __format__ method includes an additional parameter, format_spec, which defines how the object should be formatted. Below is an example showcasing custom specifications:
We define two specifications: 'i' for initials and 'C' for capitalization. The __format__ method will return the student's initials or the uppercase version of their name based on the specified format.
# Custom specifications for formatting
def __format__(self, format_spec):
if format_spec == 'i':
return self.name[0] # Return initialselif format_spec == 'C':
return self.name.upper() # Return capitalized namereturn self.name # Default formatting
Conclusion
In this article, we examined three key special methods essential for string formatting in Python:
- The __repr__ method provides a string representation that, when possible, should be a valid Python expression for object reconstruction.
- The __str__ method delivers descriptive information about the object.
- The __format__ method allows for custom formatting specifications beyond standard representations.
Watch this video to learn how to format strings in Python 3, enhancing your understanding of string representation.
In this tutorial, dive deeper into advanced string formatting techniques for dictionaries, lists, numbers, and dates.