Python Constructors
Table of Contents
In the last lesson, we learned about Python Classes and Objects. After learning how to create classes and objects, let us learn about constructors in Python. A constructor is a special method that runs when we create an object.
In this tutorial, we will learn what constructors are, how to use them, and why they are useful.
What Is a Constructor in Python?
A constructor is a special method that runs automatically when we create an object from a class.
In Python, the constructor is called __init__. It has double underscores before and after the name.
We use it to set up the initial state of the object.
class Dog: def __init__(self, name, age): self.name = name self.age = age
my_dog = Dog("Buddy", 3)print(my_dog.name)print(my_dog.age)Output
Buddy3When we write Dog("Buddy", 3), Python creates a new object and calls __init__ with self as the new object, and "Buddy" and 3 as the other arguments.
Why Do We Need a Constructor in Python?
Without a constructor, we have to set attributes manually after creating an object.
class Dog: pass
my_dog = Dog()my_dog.name = "Buddy"my_dog.age = 3That works. But it is repetitive. And we might forget to set an attribute.
With a constructor, we set the attributes right when we create the object. That is cleaner and safer.
How to Use the init Method in Python
The __init__ method takes self as the first parameter. Then it can take any other parameters we want.
class Person: def __init__(self, name, age, city): self.name = name self.age = age self.city = city
person = Person("Alex", 25, "London")print(person.name)print(person.age)print(person.city)Output
Alex25LondonThat is how we use a constructor.
Default Values in Constructors
We can give default values to constructor parameters.
class Person: def __init__(self, name, age=0, city="Unknown"): self.name = name self.age = age self.city = city
person1 = Person("Alex")person2 = Person("Sam", 30)person3 = Person("John", 22, "London")
print(person1.name, person1.age, person1.city)print(person2.name, person2.age, person2.city)print(person3.name, person3.age, person3.city)Output
Alex 0 UnknownSam 30 UnknownJohn 22 LondonThat is useful when some values are optional.
A Real-Life Example β Student Class
Letβs create a student class with a constructor.
class Student: def __init__(self, name, age, course): self.name = name self.age = age self.course = course
def introduce(self): print(f"Hi, I am {self.name}. I am {self.age} years old. I study {self.course}.")
student1 = Student("Alex", 20, "Computer Science")student2 = Student("Sam", 22, "Mathematics")
student1.introduce()student2.introduce()Output
Hi, I am Alex. I am 20 years old. I study Computer Science.Hi, I am Sam. I am 22 years old. I study Mathematics.That is a clean way to create and use objects.
Types of Constructors in Python
Python has only one type of constructor. The __init__ method.
But there is also __new__. It is called before __init__. It creates the object. But we rarely need to use it. For now, just remember that __init__ is the constructor we use.
Common Mistakes with Constructors in Python
Mistake 1: Forgetting self
class Dog: def __init__(name): self.name = nameThis gives an error. Because self is missing.
Fix:
class Dog: def __init__(self, name): self.name = nameMistake 2: Forgetting to pass arguments
class Dog: def __init__(self, name): self.name = name
my_dog = Dog()This gives an error. Because name is required.
Fix:
my_dog = Dog("Buddy")Mistake 3: Using the wrong name for the constructor
class Dog: def init(self, name): self.name = nameThis does not work as a constructor. Because it is not __init__. It is just a regular method.
Fix:
class Dog: def __init__(self, name): self.name = nameNext, we will learn about Python Instance Variables and store separate data on each object.