Get startedGet started for free

Class anatomy: the __init__ constructor

1. Class anatomy: the __init__ constructor

Welcome back! In the previous lesson, you learned how to define methods and attributes in classes.

2. Methods and attributes

You learned that methods are functions within class with a special first argument self, and that attributes are created by assignment and referred to using the self variable within methods. In the exercises, you created an Employee class, and for each attribute you wanted to create, you defined a new method, and then called those methods one after another. This could quickly get unsustainable if your classes contain a lot of data.

3. Constructor

A better strategy would be to add data to the object when creating it, like you do when creating a numpy array or a DataFrame. Python allows you to add a special method called the constructor that is automatically called every time an object is created. The method has to be called underscore underscore init underscore underscore (the exact name and double underscores are essential for Python to recognize it). Here we define the init method for the customer class. The method takes on one argument, name, of course in addition to the self argument that should be there for any method. In the body of the method, we create the name attribute, set its value to the name parameter, and print a message. So now, we can pass the customer name in the parentheses when creating the customer object, and the init method will be automatically called, and the name attribute created.

4. Add parameters

We can add another parameter -- say, account balance -- to the init method, and create another attribute -- also called balance, that will also be initialized during object creation. We can now create a customer by calling Customer with two parameters in parentheses.

5. Default arguments

The init constructor is also a good place to set the default values for attributes. For example, here we set the default value of the balance argument to 0, so we can create a Customer object without specifying the value of the balance, but the attribute is created anyway, and is initialized to the default value 0.

6. The right place to define attributes

So, there are two ways to define attributes: we can define an attribute in any method in a class; and then calling the method will add the attribute to the object. Alternatively, we can define them all together in the constructor. If possible, try to avoid defining attributes outside the constructor. Your class definition can be hundreds lines of code long, and the person reading it would have to comb through all of them to find all the attributes. Moreover, defining all attributes in the constructor ensures that all of them are created when the object is created, so you don't have to worry about trying to access an attribute that doesn't yet exist. All this results in more organized, readable, and maintainable code.

7. Best practices

While we're on the subject of best practices, let's quickly go over a few more conventions that will make your code more reader-friendly.

8. Best practices

To name your classes, use camel case, which means that if your class name contains several words, they should be written without delimiters, and each word should start with a capital letter. For methods and attributes, it's the opposite -- words should be separated by underscores and start with lowercase letters.

9. Best practices

Here's a secret: the name "self" is a convention. You could actually use any name for the first variable of a method, it will always be treated as the object reference regardless. For example, if you are a Java programmer, you might be tempted to use "this", and if you are me, you might be tempted to use "kitty". Don't do it, and always use "self".

10. Best practices

Finally, classes, like functions, allow for docstrings which are displayed when help() is called on the object. Remember the first lesson of the course, and use the docstrings to make the life of the person using your class easier.

11. Let's practice!

Now, let's practice creating constructors!