- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Basics in English / of Python Class Design
Python Class – Key Concepts & Syntax
Concepts covered: What is a class, keywords, syntax, constructor, parameter types, and function types.
1. What is a Class?
A class is a blueprint or template used to create objects. It groups related data (variables) and behaviour (functions) into a single unit.
Class → Blueprint / design
Object (Instance) → Real thing created from the blueprint
OOP Concepts used in classes:
Encapsulation – keeping data and functions together
Inheritance – reusing code from a parent class
Polymorphism – same function name, different behaviours
Abstraction – hiding internal complexity, showing only what is required
2. Important Keywords & Concepts in Python Classes
class – used to define a new class.
self – refers to the current object (instance) inside class methods.
__init__ – constructor method, called automatically when an object is created.
object – base class of all classes in Python (in practice we often just write class MyClass:).
@classmethod – decorator for class methods which receive cls instead of self.
@staticmethod – decorator for static methods that don’t use self or cls.
@property – decorator to create getter-like methods that work like attributes.
3. Basic Class Syntax
General structure of a simple class in Python:
class ClassName:
# class variables (optional)
class_variable = 10
def __init__(self, param1, param2):
# instance variables
self.attr1 = param1
self.attr2 = param2
def instance_method(self):
return self.attr1
Creating an object from the class:
obj = ClassName("value1", "value2")
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).