- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Basics in English / of Class Variables
4. Constructor – __init__
The constructor is a special method named __init__ which runs automatically when we create an object. It is used to initialize instance variables.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
# object creation
s1 = Student("Rahul", "Python")
First parameter must be self (reference to current object).
We pass values at object creation – they come into the constructor parameters.
Inside constructor we store data into self attributes (instance variables).
5. Parameter Types in Class Methods
Class methods (including constructor) can use all normal Python parameter types.
Positional parameters
Values are passed in order.
Default parameters
We give a default value in the method definition.
Keyword parameters
We pass values using name=value style.
*args
Collects extra positional arguments as a tuple.
**kwargs
Collects extra keyword arguments as a dictionary.
class Order:
def __init__(
self,
order_id, # positional
status = "NEW", # default
*items, # *args
**extra_info # **kwargs
):
self.order_id = order_id
self.status = status
self.items = items
self.extra_info = extra_info
6. Function Types inside a Class
Inside a class, functions are called methods. Common types:
Instance methods
Most common. First parameter is self. Can access and modify object data.
Class methods (@classmethod)
First parameter is cls (class itself). Often used as alternative constructors.
Static methods (@staticmethod)
No self or cls. A normal function kept inside class for grouping.
Special / dunder methods (like __str__, __len__)
Used to customize behaviour of built-in functions and operators.
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
# Instance method
def deposit(self, amount):
self.balance += amount
# Class method
@classmethod
def from_string(cls, data):
owner, bal_str = data.split(",")
return cls(owner, float(bal_str))
# Static method
@staticmethod
def is_valid_amount(amount):
return amount >= 0
7. Quick Summary
Use class to define a blueprint.
Use __init__ as constructor to initialize data.
Always keep self as first argument in instance methods.
Use different parameter types (*args, **kwargs) for flexible input.
Use instance / class / static methods based on what you need to access (object, class, or none).
8. Class Variable vs Instance Variable
Class Variable → Shared by all objects of the class. Defined inside the class but outside any method.
Stored in class memory, not in object memory.
Changing a class variable affects all objects.
Used for constant values or shared information.
Instance Variable → Unique to each object. Defined inside __init__ using self.
Stored separately for each object.
Changing instance variable affects only that object.
Used to store object-specific data.
class Employee:
company = "TCS" # class variable
def __init__(self, name, salary):
self.name = name # instance variable
self.salary = salary # instance variable
9. Instance Functions vs Class Functions
Instance Function (Instance Method)
Most common method type.
First parameter is self.
Used to access instance variables.
Can also access class variables.
Class Function (Class Method)
Defined using @classmethod.
First parameter is cls (the class itself).
Can access class variables but not instance variables.
Used for factory methods (alternative constructors).
class Car:
wheels = 4 # class variable
def __init__(self, brand):
self.brand = brand # instance variable
# Instance function
def show_details(self):
return f"Brand: {self.brand}, Wheels: {Car.wheels}"
# Class function
@classmethod
def change_wheels(cls, count):
cls.wheels = count