Understanding construtor vs Attributes in python class

@amitmund June 04, 2026

Constructor vs Attributes

The constructor and attributes are related, but they are not the same thing.


Think of it this way

Constructor = A Special Method

class Student:

    def __init__(self):
        self.name = "John"
        self.age = 20

Here:

def __init__(self):

is the constructor.

The constructor's job is to initialize (set up) the object when it is created.


Attributes = Data Stored in the Object

Inside the constructor:

self.name = "John"
self.age = 20

These are attributes (instance variables).

So:

Code Meaning
__init__() Constructor
self.name Attribute
self.age Attribute

Example 1: Fixed Values

class Student:

    def __init__(self):
        self.name = "John"
        self.age = 20

Create object:

s1 = Student()

Object becomes:

s1
├── name = John
└── age = 20

Every object gets the same values.

s1 = Student()
s2 = Student()

Both contain:

name = John
age = 20

Example 2: Values Passed from Outside

class Student:

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

Create object:

s1 = Student("John", 20)

Python does:

Student.__init__(s1, "John", 20)

Inside constructor:

self.name = name

becomes:

s1.name = "John"

and

self.age = age

becomes:

s1.age = 20

Object:

s1
├── name = John
└── age = 20

Create another object:

s2 = Student("Alice", 22)

Object:

s2
├── name = Alice
└── age = 22

Now each object can have different values.


What's the Difference?

Version 1

class Student:

    def __init__(self):
        self.name = "John"
        self.age = 20

Values are hardcoded.

Every object gets:

John
20

Version 2

class Student:

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

Values come from outside.

Student("John", 20)
Student("Alice", 22)
Student("Bob", 18)

Each object gets different data.


The Correct Terminology

❌ Incorrect

Passing values to attributes is called a constructor.

✅ Correct

__init__() is the constructor.

The constructor initializes attributes.

Values can be assigned to attributes inside the constructor.


Real-Life Analogy

Imagine a student admission form.

The form itself is the constructor.

def __init__(self, name, age):

The fields on the form are:

name
age

The stored information becomes attributes:

self.name
self.age

When someone fills:

Name = John
Age = 20

the constructor stores that data in the object.


One More Example

class Car:

    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

Create:

c1 = Car("Toyota", "White")
c2 = Car("BMW", "Black")

Result:

c1
├── brand = Toyota
└── color = White

c2
├── brand = BMW
└── color = Black

Here:

  • __init__() → Constructor
  • brand, color → Constructor Parameters
  • self.brand, self.color → Attributes (Instance Variables)

A Useful Rule to Remember

Constructor = The setup method (__init__())

Attributes = The data stored in the object (self.name, self.age, etc.)

Constructor Parameters = Values passed when creating the object ("John", 20)

0 Likes
45 Views
0 Comments

Filters

No filters available for this view.

Reset All