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

Python Classes and Objects in Hindi

What is Python Classes and Objects in Hindi

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 : python मे __init__ method को constructor कहा जाता है, जब हम object को बनाते है तो __init__ method automatic call हो जाता है, इसका use  किसी object को initialize करने के लिए किया जाता है। 

Parameters : methods मे हम जो value pass करते है उन्हे parameters कहा जाता है।

Self  : जब हम कोई object create करते है तो python automatically एक value (parameter) pass कर देता है, हमे बस इसे define करना होता है जिसके लिए हम self लिखते है but आप self की जगह कोई भी valid word लिख सकते है, आमतौर पर सिर्फ self का ही use किया जाता है, self किसी object के current instance को refer करता है, self का use instance attributes और methods को access करने के लिए किया जाता है। 

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
Python Function

Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...

Python Dictionary

Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...

Python Set

Table of Contents All Chapters 1. Python Set 2. Iterate Set items 3. Set Methods      3.1. Add items...

Python Tuple

Table of Contents All Chapters 1. Python Tuple 2. Access Tuple Items 3. Update Tuple 4. Iterate Tuple Items 5....

Python List

Table of Contents All Chapters 1. Python List 2. Nested List 3. Range List 4. List Indexing 5. list Methods...

Python String

Table of Contents All Chapters 1. Python String 2. String Indexing 3. String Slicing 4. String Concatenation 5. String Formatting...

Python Loops

Table of Contents All Chapters 1. Python Loops 2. While Loop      2.1. Nested While Loop 3. For Loop...

Edit Template
Scroll to Top