Home >> Python Tutorial >> Python Classes and Objects
Edit Template
Edit Template

Python Classes and Objects in Hindi

What is Python Classes and Objects

Classes and Objects किसी भी Object-Oriented Programming के core concepts होते है, इस tutorial मे हम समझेंगे की Python मे Classes and Objects का use कैसे किए जाता है।

What is Class in Python

एक class किसी object का blueprint होता है, ये object के structure and behavior को define करता है। एक class मे variables and functions define होते है।

How to Create a Class in Python

किसी class को create करने के लिए “class” keyword का use किया जाता है।   

Attributes : class के अंदर जो variable create किए जाते है उन्हे attributes कहा जाता है। attributes के 2 types होते है,

  1. Class Attributes : ऐसे attributes जो class के अंदर लेकिन methods के बाहर create किए जाते है उन्हे class attributes कहा जाता है, ये सभी objects के लिए same होते है। 
  2. Instance Attributes : ऐसे attributes जो किसी class के methods मे create किए जाते है उन्हे Instance attributes कहा जाता है, ये हर objects के लिए अलग-अलग हो सकते है।

Constructor : constructor वो method होता है जिससे python को पता चलता है की कोई भी class कहा से start हो रहा है, python मे एक method होता है __init__ जिसे constructor कहा जाता है, ये एक Dunder Method है इसलिए जब हम object को बनाते है तो __init__ method automatic call हो जाता है, इसका use  किसी object को initialize करने के लिए किया जाता है।

Self  : जब हम किसी class का object create करते है तो python automatically उस object को class मे as a argument pass कर देता है, कर देता है, जिसके लिए हमे हर method मे एक self parameter बनाना होता है but आप self की जगह कोई भी valid word लिख सकते है, आमतौर पर सिर्फ self का ही use किया जाता है। 

Methods : class के अंदर जो भी functions define किए जाते है उन्हे methods कहा जाता है, ये 3 types के होते है,

  1. Instance Methods : ऐसे methods जो object के साथ काम करते है, उन्हे instance method कहते है। इसमे सबसे पहला parameter “self” होता है। 
  2. Class Methods : ऐसे methods जो class के साथ काम करते है, उन्हे class method कहते है। इसमे सबसे पहला parameter “cls” (Class) होता है। class method को create करने के लिए @classmethod decorator का use  करते है। 
  3. Static Methods : ये method किसी भी class या instance से नहीं जुड़े होते है और ना ही कोई special parameter ( जैसे self या cls ) लेते है। static method को create करने के लिए @staticmethod decorator का use  करते है।

SYNTAX :

class ClassName:
       # Class attributes

       # Constructor
       def __init__(self, parameters):
            # Instance attributes

       # Methods
       def method_name(self):
            # Method body

What is Object in Python

Object किसी class का instance होता है। ये class के structure और behavior के हिसाब से work करता है।

Object को create करने के लिए Class के Constuctor को call किया जाता है। 

Object के Attributes और Methods को access करने के लिए dot notation ( . ) का use किया जाता है। 

SYNTAX : 

object_name = ClassName(arg1, arg2)

🐍
Example.py
Copy to clipboard
class Employee:
    # Class Attribute
    company = "Tech Corp"
    
    # Constructor
    def __init__(self, name, salary):
        # Instance Attributes
        self.name = name
        self.salary = salary
    
    # Instance Method
    def show_details(self):
        print(f"Name: {self.name}, Salary: {self.salary}")
    
    # Class Method
    @classmethod
    def change_company(cls, new_name):
        cls.company = new_name
    
    # Static Method
    @staticmethod
    def calculate_bonus(salary):
        return salary * 0.1

# Usage
emp1 = Employee("Rahul", 50000)
emp1.show_details()  # Instance method

Employee.change_company("New Tech")  # Class method
print(Employee.company)  # "New Tech"

bonus = Employee.calculate_bonus(50000)  # Static method
print(f"Bonus: {bonus}")  # 5000

Some Important Points of Classes and Objects

Modify Object Properties

हम object के properties को directly modify कर सकते है, इसके लिए हमे dot notation ( . ) का use करना होता है।

🐍
Example 2.py
Copy to clipboard
class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

my_car = Car("Tata", "Nexon")
print(my_car.brand)  # Tata

# Property modify
my_car.brand = "Mahindra"
my_car.model = "XUV700"
print(my_car.brand)  # Mahindra

Del Keyword

del keyword का use करके हम किसी भी object या object properties को delete कर सकते है। 

🐍
Example 3.py
Copy to clipboard
 class Student:
    def __init__(self, name, roll_no):
        self.name = name
        self.roll_no = roll_no

s1 = Student("Priti", 101)

# Property delete
del s1.roll_no
print(s1.roll_no)  # AttributeError

# Delete object —--------------

class Temp:
    def __init__(self):
        print("Object created")
    
    def __del__(self):
        print("Object deleted")

obj = Temp()  # Object created
del obj       # Object deleted

Pass Keyword

जब हम कोई empty class या function को create करते है तो यह error देता है, but हम pass keyword का use करके error को avoid कर सकते है।

🐍
Example 4.py
Copy to clipboard
class EmptyClass:
    pass 

def future_function():
    pass

# Error नहीं आएगा क्योंकि pass ने empty block को valid बना दिया

उम्मीद करते है कि आपको Python Classes and Objects अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Fundamentals
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
Dunder Methods

Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods   Python Tutorial Python Introduction Identation &...

Encapsulation

Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers      2.1. Public Members      2.2. Protected Members...

Lambda Function

Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...

Nested Function

Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...

Context Manager

Table of Contents All Chapters 1. Context Manager 2. with Statement      2.1. __enter__() Method      2.2. __exit__()...

File Pointer

Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods      2.1. tell() Method      2.2....

File Handling

Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...

Edit Template
Scroll to Top