
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Classes and Objects किसी भी Object-Oriented Programming के core concepts होते है, इस tutorial मे हम समझेंगे की Python मे Classes and Objects का use कैसे किए जाता है।
एक class किसी object का blueprint होता है, ये object के structure and behavior को define करता है। एक class मे variables and functions define होते है।
किसी class को create करने के लिए “class” keyword का use किया जाता है।
Attributes : class के अंदर जो variable create किए जाते है उन्हे attributes कहा जाता है। attributes के 2 types होते है,
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 के होते है,
SYNTAX :
class ClassName:
# Class attributes
# Constructor
def __init__(self, parameters):
# Instance attributes
# Methods
def method_name(self):
# Method body
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)
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
हम object के properties को directly modify कर सकते है, इसके लिए हमे dot notation ( . ) का use करना होता है।
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 का use करके हम किसी भी object या object properties को delete कर सकते है।
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
जब हम कोई empty class या function को create करते है तो यह error देता है, but हम pass keyword का use करके error को avoid कर सकते है।
class EmptyClass:
pass
def future_function():
pass
# Error नहीं आएगा क्योंकि pass ने empty block को valid बना दिया
उम्मीद करते है कि आपको Python Classes and Objects अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...