You already got to know your first Python dunder method (__init__()) in a previous lesson. Python defines many of them for its built-in data structures, and in this lesson, you'll get to know some common ones.
Dunder Definition
But first, what's with that name?
These methods are officially called double-underscore methods, and dunder is a convenient abbreviation for "double underscore". Their naming comes from how all of them are written by using double-underscores before and after the method (__method__()).
If you like this reference, you can think of them as "Dundies":
But that's not really pythonic and might cause some confusion, so maybe it's better to stick with dunder methods. :-)
Commonly Implemented Python Dunder Methods
In the following lessons, you'll take a quick look at some commonly used dunder methods that you might encounter when reading other people's code. By the end of this lesson, you'll be more familiar with their names, and you'll have a general idea of what they are used for.
Common Dunder Methods
Specifically, you'll look at the following dunder methods:
__init__()for constructing instances__str__()for user-focused documentation__repr__()for developer-focused documentation__add__()for adding instances with the+operator
More About Dundies ;)
As mentioned, there are many more dunder methods, and every class can define its own. Not all data types in Python share the same dunder methods, as some only make sense for specific use cases.
You've also learned that dunder methods aren't really that special; they're just normal methods that are meant to stay hidden from your users. The reason to know about them is that you can implement commonly shared dunder methods to make your custom classes blend in intuitively with built-in Python data types.
Below, you'll start adding each of the mentioned dunder methods to your Ingredient() class to build it out more and make your custom class feel almost like a native Python object.
Construction __init__()
You've already built the __init__() method to be able to pass arguments to your constructor and create different instances of your Ingredient() class:
class Ingredient:
"""Models a food item used as an ingredient."""
def __init__(self, name, amount):
self.name = name
self.amount = amount
def expire(self):
"""Expires the ingredient."""
print(f"whoops, these {self.name} went bad...")
self.name = "expired " + self.name
__init__() is the constructor method for your class. It's used to initialize a class instance either with default values or with arguments that you passed when instantiating a new object.
In the next lesson, you'll learn about two Python dunder methods that you can use to document your class objects and make them easier for others to work with.
Additional Resources
Summary: Python Dunder Methods
- Dunder is a convenient abbreviation for double underscore
- The init method is one of the most common dunder methods since it instantiates all new objects
Example Dunder Methods
__init__()for constructing instances__str__()for user-focused documentation__repr__()for developer-focused documentation__add__()for adding instances with the+operator