Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22465

"Mathspp Pydon'ts": Object initialisation with __init__

$
0
0

Learn about the dunder method __init__, responsible for initialising class instances.

A blank cube being passed to the dunder method `__init__` and coming out fully initialised, represented by a scrambled Rubik's cube.Background photo by Олександр К on Unsplash

Introduction

The dunder method__init__ is probably the first dunder method that Python programmers learn about. When you first start defining your own classes, you are taught that you need to initialise your objects inside this crazy magic method called __init__, and you don't really understand why that method has such a weird name.

When I first learned about __init__, I thought it was this magical method that worked in an obscure way... But that is not true! In what follows, I will try to demystify what __init__ does and how it works.

You can now get your free ✨ copy of the ebook “Pydon'ts – Write beautiful Python code” on Gumroad to help support my Python 🐍 content.

What is __init__ for?

The dunder method __init__ is the method of your class that is responsible for initialising your object upon creation. Just like the diagram above hints at, your method __init__ receives a fresh new instance of your class, and it is at that point that you are free to customise it and adapt it, according to the arguments that were passed in when the first instance was created.

For example, suppose you are a freelancer and you want to create a simple system to keep track of all your clients. Maybe you would create a class Client, and each client would have some information associated with them, say, their name and their email.

Thus, to create a new instance of Client, you would need to pass in the name and email:

class Client:
    # ...

# Create two clients:
alice = Client("Alice", "alice@example.com")
bob = Client("Bob", "bob@example.com")

The rationale is that both the name and the email of the client stay associated with that object, and you associate those things to the objects when you initialise them.

Let us take a closer look at the line that creates the client Alice:

alice = Client("Alice", "alice@example.com")

Notice that __init__ does not show up in that line. And yet, when that line runs, the dunder method __init__ will be called... And it will be given three pieces of information! Obviously, the dunder method __init__ will receive the string name and the string email, but there is one extra piece of information that __init__ will receive and that will actually come first.

self

The extra piece of information that __init__ receives is the object that __init__ is actually initialising!

Recall that __init__ is supposed to initialise your object upon creation, so Python will create a blank Client object, and then it will give it to you. The dunder method __init__ accepts that blank Client object, and that is what makes it possible for you to attach the name and email strings as attributes of that instance.

The diagram below depicts this: the blank cube goes into __init__, just like the string name and the string...


Viewing all articles
Browse latest Browse all 22465

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>