ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग की बुनियाद

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

क्लास परिभाषित करना

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
    self.height = 0

# This invokes a call to __init__
john = Person("John Casey", 38)
  • class कीवर्ड से एक क्लास परिभाषित करें
  • __init__ कंस्ट्रक्टर है, और नया क्लास ऑब्जेक्ट बनने पर कॉल होता है
  • self क्लास की मौजूदा instance को संदर्भित करता है
Intermediate Object-Oriented Programming in Python

इंस्टेंस एट्रिब्यूट्स

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
# Create an instance of Person
sarah = Person("Sarah Walker", 31)
sarah.age  # Retrieve the age instance attribute
  • क्लास के किसी ऑब्जेक्ट से संबद्ध
  • self.<attribute-name> सिन्टैक्स से सेट और पढ़ें
  • <object-name>.<attribute-name> से एक्सेस करें
Intermediate Object-Oriented Programming in Python

क्लास एट्रिब्यूट्स

  • सीधे क्लास से जुड़े होते हैं
  • क्लास ऑब्जेक्ट के बिना भी पढ़े जा सकते हैं
  • वह डेटा रखते हैं जो सभी क्लास ऑब्जेक्ट्स के लिए समान हो
class Person:
  residence = "Planet Earth"
  ...
# Accessed without an instance
print(Person.residence)
Planet Earth
Intermediate Object-Oriented Programming in Python

इंस्टेंस मेथड्स

class Person:
  ...
  def introduce(self):
      print(f"Hello, my name is {self.name}")
chuck = Person("Chuck", 32)
chuck.introduce()  # Called on a Person object

$$

  • कॉल करने के लिए क्लास का ऑब्जेक्ट होना चाहिए
  • पहले पैरामीटर के रूप में self लेते हैं
Intermediate Object-Oriented Programming in Python

क्लास मेथड्स

class Person:
  @classmethod
  def wake_up(cls):
      print("Time to start your day!")
# Calling a class method
Person.wake_up()

$$

  • @classmethod डेकोरेटर से चिह्नित
  • कॉल करने के लिए क्लास ऑब्जेक्ट आवश्यक नहीं
Intermediate Object-Oriented Programming in Python

इनहेरिटेंस

Inheritance से क्लासों के बीच कोड री-यूज़ होता है.

  • चाइल्ड क्लास (Employee) पैरेंट (Person) की सभी कार्यक्षमता इनहेरिट करता है
  • "is-a" रिलेशनशिप
  • अतिरिक्त कार्यक्षमता जोड़ सकता है
    • एट्रिब्यूट्स
    • मेथड्स
class Person:
  def __init__(self, name, age):
    self.name
    self.age = age

  def introduce(self):
      print(f"Hello, my name is {self.name}")
class Employee(Person):
  def __init__(self, name, age, title):
    Person.__init__(self, name, age)
    self.title = title

  def change_position(self, new_title):
      self.title = new_title
Intermediate Object-Oriented Programming in Python

इनहेरिटेंस

lester = Employee("Lester", 26, "Technician")
lester.introduce()  # Inherited from Person
print(lester.title)
Hello, my name is Lester
Technician
lester.change_position("Cashier")
print(lester.title)
Cashier
Intermediate Object-Oriented Programming in Python

super()

class Employee(Person):
  def __init__(self, name, age, title):
    # Uses name of the parent class
    Person.__init__(self, name, age)

    self.title = title
  ...
  • क्लास नाम, __init__()

super() का उपयोग

class Employee(Person):
  def __init__(self, name, age, title):
    # super(), instead of class name
    super().__init__(name, age)

    self.title = title
  ...
  • super(), __init__()
  • self पास करने की आवश्यकता नहीं
Intermediate Object-Oriented Programming in Python

ओवरराइडिंग

चाइल्ड पैरेंट से मिले मेथड को एक नए तरीके से लागू करता है.

class Employee(Person):
  ...
  def introduce(self):
      print(f"""My name is {self.name},
        I am a {self.title}""")
lester = Employee("Lester", 26, "Technician")
lester.introduce()
My name is Lester, I am a Technician
Intermediate Object-Oriented Programming in Python

ओवरलोडिंग

class Person:
  def __init__(self, name):
      self.name = name

  def __eq__(self, other):
      return self.name == other.name

$$

  • किसी क्लास के लिए Python ऑपरेटर्स के व्यवहार को कस्टमाइज़ करें
  • __eq__() का उपयोग == को ओवरलोड करने के लिए होता है

$$

$$

$$

$$

# Compare two Person objects
chuck = Person("Charles Carmichael")
charles = Person("Charles Carmichael")
print(chuck == charles)
True
Intermediate Object-Oriented Programming in Python

अभ्यास करते हैं!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...