Learning Guides
Class and Object in Python: Complete Guide
Quick answer: Learn Python classes and objects including attributes, methods, the __init__ constructor, self, and the difference between class and instance variables.
What is a Class?
A class is a blueprint for creating objects, defining what attributes and methods those objects will have. An object is a specific instance created from that class.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def introduce(self):
return f"I am {self.name}, studying {self.course}"
s1 = Student("Priya", "Data Science")
print(s1.introduce()) # I am Priya, studying Data Science
The __init__ Constructor
__init__ runs automatically when a new object is created, typically used to set up the object's initial attributes.
class Course:
def __init__(self, name, fee):
self.name = name
self.fee = fee
c = Course("Data Analytics", 65000)
print(c.name, c.fee)
Understanding self
self refers to the specific object the method is being called on. It must be the first parameter of every instance method, though Python passes it automatically, so you never supply it explicitly when calling the method.
class Counter:
def __init__(self):
self.count = 0
def increment(self):
self.count += 1 # self.count refers to THIS object's count
c1 = Counter()
c2 = Counter()
c1.increment()
c1.increment()
print(c1.count) # 2
print(c2.count) # 0, unaffected by c1
Class Variables vs Instance Variables
class Student:
school = "Fireblaze AI School" # class variable, shared by all instances
def __init__(self, name):
self.name = name # instance variable, unique per object
s1 = Student("Aman")
s2 = Student("Neha")
print(s1.school, s2.school) # both print "Fireblaze AI School"
print(s1.name, s2.name) # Aman, Neha (different per object)
Instance Methods
A method is a function defined inside a class that operates on a specific object's data, accessed through self.
class BankAccount:
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
print("Insufficient balance")
account = BankAccount(1000)
account.deposit(500)
account.withdraw(200)
print(account.balance) # 1300
Common Interview Questions
What is the difference between a class and an object?
A class is the blueprint or template. An object is a specific instance created from that class, with its own actual data.
Why does every method need self as its first parameter?
self lets the method know which specific object's data to operate on, since a class can have many objects, each with its own independent state.
What is the difference between a class variable and an instance variable?
A class variable is shared across every object created from the class. An instance variable is specific to each individual object.
FAQ
Frequently Asked Questions
What is the difference between a class and an object in Python?
A class is a blueprint defining attributes and methods. An object is a specific instance created from that class, holding its own actual data.
What does __init__ do in a Python class?
It is the constructor method that runs automatically when a new object is created, typically used to set the object's initial attribute values.
Why do Python methods need self as the first parameter?
self refers to the specific object the method is being called on, letting the method access and modify that particular object's data.
What is the difference between a class variable and an instance variable?
A class variable is shared by every object created from the class. An instance variable is unique to each individual object.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin