HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

2) Classes, Objects, and Methods Lesson

Python __init__() Arguments

5 min to complete · By Martin Breuss

Using __init__() becomes much more useful once you understand that it behaves very similarly to any function that you've already gotten to know. Just like any familiar function, Python __init__() can take arguments.

Pass Arguments to Python __init__()

When you pass arguments to your class constructor, you can assign their values to instance variables. To be able to create ingredients with different names, you'll next change the dunder init method of your Ingredient() class to take a name parameter:

class Ingredient:
    """Models a food item used as an ingredient."""

    def __init__(self, name):
        self.name = name

The code looks nearly the same as it did before. But, additionally to the still mysterious self, you are now using name as a second parameter to __init__().

Colorful illustration of a light bulb

Info: You'll learn all about what self is and why you need it for your methods in an upcoming lesson.

Method Body

In the method body, you're assigning the value of name to a new instance variable, self.name.

When you create a new instance of your class, a process called instantiation, you now need to pass a value for name as an argument in the same way that you'd pass it to a function call:

i = Ingredient("peas")

This binds the argument value to your instance variable self.name. That instance variable and its value belong to the specific object instance you just created, and you can now access it through its namespace:

print(i.name)  # OUTPUT: peas

Printing i.name will display the name you've passed as an argument to the class constructor Ingredient().

Type Error

If you try to instantiate a new object without passing an argument when your dunder init Python method declares that the object needs one, then the instantiation will fail with an error message:

j = Ingredient()
# OUTPUT: TypeError: __init__() missing 1 required positional argument: 'name'

Remember that the error message is your friend that points you in the right direction and helps you figure out what you might have missed in your code. In this case, it's telling you that you forgot to pass the required argument name to your class constructor.

Colorful illustration of a light bulb

Info: You can see that the error message mentions __init__() even though you haven't explicitly called this method anywhere. As mentioned earlier, dunder init is called every time you instantiate a new object with the class constructor, in this case Ingredient().

Why Use Arguments

Arguments have proven extremely helpful for building your functions, and you'll see that this small tweak also makes your code infinitely more useful for classes. Instead of only being able to make carrots, you can now create any ingredient that you can name:

c = Ingredient("cauliflower")
b = Ingredient("broccoli")
d = Ingredient("dandelion")

print(c.name, b.name, d.name)
# OUTPUT: cauliflower broccoli dandelion

Just like functions, methods can take more than one argument.

In the following section, you'll take a look at how to make your objects do something through instance methods.

Illustration of a lighthouse

Tasks

  • Think of two attributes to an ingredient that you'd like your Ingredient() objects to have.
  • Add these attributes as instance variables to your class definition, following the name example shown above.

Summary: Python __init__() Arguments

  • The __init__() can have custom parameters
  • Custom parameters must be passed to the constructor when instantiating a new object
  • Dunder init gets called implicitly when you instantiate an object
  • Custom instance variables allow new objects to have unique values during declaration