What are dunder methods in Python?

What are dunder methods in Python?

Where the name comes from?Permalink

First, let's explain the name. What does dunder mean? It is a shortcut for Double Undersore.

Dunder methods are methods whose name starts with two underscores and ends with two underscores. The most common is probably the __init__ method which we use to initialise an instance of the class.

class MyClass:
    __init__(self):
        # do some initialization

What’s special about dunder methods?Permalink

There are many dunder methods in Python. How are they unique?

Dunder methods are defined by Python language and are called internally by Python or during some operations. For example, the __init__ method is called by Python when we create an instance of a class.

We should not call dunder methods directly on instances (objects) from the outside. So we should not do things like this:

numbers = [1, 2, 3]
numbers.__len__()
# outputs: 3

Rather, we ought to use the len() function, which calls __len__() method for us:

numbers = [1, 2, 3]
len(numbers)
# outputs: 3

We must not invent any new dunder methods ourselves. They could interfere with the internal working of the Python now or in the future.

However, we can use existing, already defined dunder methods as documented.

Dunder methods in actionPermalink

Let's create a new class FullName:

class FullName:
    def __init__(self, first, last):
        self.first = first
        self.last = last

In the code above, we define a new class and use the __init__ method to store the names in instance variables. Let's see how it works.

# create an instance of the class
friend = FullName('John', 'Doe')

friend.first
# outputs: 'John'

friend.last
# outputs: 'Doe'

friend
# outputs: <__main__.FullName object at 0x10d80ebf0>

We can see our class works as expected. After creating an instance, we can get back the first and last names we've put in.

When we type friend, the variable's name, we get a description of our instance and its address in the memory.

That is a string representation of the instance. We can also get it by using the repr() function:

repr(friend)
# outputs: <__main__.FullName object at 0x10d80ebf0>

But we can change that. Say we want to use first and last name as a string representation of the instance. How would we do it? We can use another dunder method, __repr__:

class FullName:
    def __init__(self, first, last):
        self.first = first
        self.last = last

    def __repr__(self):
        return self.first + " " + self.last

Check if it works:

friend = FullName('John', 'Doe')

friend
# outputs: 'John Doe'

repr(friend)
# outputs: 'John Doe'

We could also call __repr__ on the instance itself:

friend.__repr__()
# outputs: 'John Doe'

But as we said before, we should not do it. Dunder methods are internal to classes and their instances; we should not call them from the outside.

You might also like

Join the newsletter

Subscribe to get new articles about Python, code and programming into your inbox!