
Table of Contents All Chapters 1. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
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 : 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 के होते है,
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. Classes and Objects 2. Class in Python 3. Class Structure 3.1. Attributes...
Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...
Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...
Table of Contents All Chapters 1. Python Set 2. Iterate Set items 3. Set Methods 3.1. Add items...
Table of Contents All Chapters 1. Python Tuple 2. Access Tuple Items 3. Update Tuple 4. Iterate Tuple Items 5....
Table of Contents All Chapters 1. Python List 2. Nested List 3. Range List 4. List Indexing 5. list Methods...
Table of Contents All Chapters 1. Python String 2. String Indexing 3. String Slicing 4. String Concatenation 5. String Formatting...
Table of Contents All Chapters 1. Python Loops 2. While Loop 2.1. Nested While Loop 3. For Loop...
Table of Contents All Chapters 1. If-else Statements 1.1. If Statement 1.2. Else Statement ...