This text is part of a Real Python tutorial by Leodanis Pozo Ramos.
As a Python developer who wants to harness the power of object-oriented programming, you’ll love to learn how to customize your classes using special methods, also known as dunder methods. A special method is a method whose name starts and ends with a double underscore. These methods have special meanings for Python.
Python automatically calls special methods as a response to certain operations, such as instantiation, sequence indexing, attribute managing, and much more. Special methods support core features in Python, so learning about them is fundamental for you as a Python programmer.
In this lesson, you’ll:
- Learn what Python’s special or dunder methods are
- Understand what’s special about special methods in Python
- Customize different behaviors of your custom classes with special methods
Getting to Know Python’s Special Methods
In Python, special methods are also called dunder methods. This latter informal terminology, dunder, refers to double underscores since Python uses double leading and double trailing underscores to name its special methods and attributes.
Note: In this tutorial, you’ll find the terms special methods and dunder methods used interchangeably. The name special method is the official name for these methods. However, dunder method is also commonly-used in the Python community. Some programmers also refer to these as magic methods, however there’s nothing magical about these methods and this term may make them seem more obscure than they really are. We won’t use the term magic method in this course!
The double underscores flag these methods as core to some Python features. They help avoid name collisions with your own methods and attributes. Some popular and well-known special methods include the following:
| Special Method | Description |
|---|---|
.__init__() |
Provides an initializer in Python classes |
.__str__() and .__repr__() |
Provide string representations for objects |
.__call__() |
Makes the instances of a class callable |
.__len__() |
Supports the len() function |
This is just a tiny sample of all the special methods that Python has. All these methods support specific features that are core to Python and its object-oriented infrastructure.
Here’s how the Python documentation defines the term special methods:
A method that is called implicitly by Python to execute a certain operation on a type, such as addition. Such methods have names starting and ending with double underscores. (Source)
There’s an important detail to highlight in this definition. Python implicitly calls special methods to execute certain operations in your code. For example, when you run the addition 5 + 2 in a REPL session, Python internally runs the following code under the hood:
>>> (5).__add__(2)
7
The .__add__() special method of integer numbers supports the addition that you typically run as 5 + 2.
Reading between the lines, you’ll realize that even though you can directly call special methods, they’re not intended for direct use. You shouldn’t call them directly in your code. Instead, you should rely on Python to call them automatically in response to a given operation.
Special methods exist for many purposes. All the available special methods support built-in features and play specific roles in the language. For example, built-in types such as lists, strings, and dictionaries implement most of their core functionality using special methods. In your custom classes, you can use special methods to make callable objects, define how objects are compared, tweak how you create objects, and more.
Note that because special methods have special meaning for Python itself, you should avoid naming custom methods using leading and trailing double underscores. Your custom method won’t trigger any Python action if its name doesn’t match any official special method names, but it’ll certainly confuse other programmers. New dunder names may also be introduced in future versions of Python.
Special methods are core to Python’s data model and are a fundamental part of object-oriented programming in Python. In the following sections, you’ll learn about some of the most commonly used special methods. They’ll help you write better object-oriented code in your day-to-day programming adventure.
Controlling the Object Creation Process
When creating custom classes in Python, probably the first and most common method that you implement is .__init__(). This method works as an initializer because it allows you to provide initial values to any instance attributes that you define in your classes.
In the following section, you’ll learn the basics of how this method works and how you can use it to customize the instantiation of your classes.