क्लास एनाटॉमी: __init__ कन्स्ट्रक्टर

Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Alex Yarosh

Content Quality Analyst @ DataCamp

Methods और attributes

  • Methods, class के अंदर function definitions होते हैं
  • पहला आर्ग्यूमेंट self होता है
  • असाइनमेंट से attributes परिभाषित करें
  • class में attributes को self.___ से संदर्भित करें

   

class MyClass:
    # function definition in class
    # first argument is self
    def my_method1(self, other_args...):
        # do things here

    def my_method2(self, my_attr):        
        # attribute created by assignment
        self.my_attr = my_attr
        ...
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Constructor

  • ऑब्जेक्ट बनाते समय उसमें डेटा जोड़ना है?
  • Constructor __init__() method हर बार ऑब्जेक्ट बनने पर कॉल होता है।
class Customer:
       def __init__(self, name):     
        self.name = name           # <--- .name attribute बनाएँ और इसे name पैरामीटर पर सेट करें
        print("The __init__ method was called")

cust = Customer("Lara de Silva") #<--- __init__ स्वतः कॉल होता है print(cust.name)
The __init__ method was called
Lara de Silva
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग
class Customer:
       def __init__(self, name, balance):  # <-- balance पैरामीटर जोड़ा

self.name = name self.balance = balance # <-- balance attribute जोड़ा print("The __init__ method was called")
cust = Customer("Lara de Silva", 1000) # <-- __init__ कॉल हुआ print(cust.name) print(cust.balance)
The __init__ method was called
Lara de Silva
1000
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग
class Customer:
       def __init__(self, name, balance=0):  #<-- balance के लिए default मान सेट करें

self.name = name self.balance = balance print("The __init__ method was called")
cust = Customer("Lara de Silva") # <-- balance स्पष्ट रूप से न दें
print(cust.name) print(cust.balance) # <-- attribute फिर भी बनता है
The __init__ method was called
Lara de Silva
0
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Methods में attributes

class MyClass:
    def my_method1(self, attr1):
        self.attr1 = attr1
        ...

    def my_method2(self, attr2):        
        self.attr2 = attr2
        ...
obj = MyClass()
obj.my_method1(val1) # <-- attr1 बना
obj.my_method2(val2) # <-- attr2 बना

Constructor में attributes

class MyClass:
    def __init__(self, attr1, attr2):
        self.attr1 = attr1
        self.attr2 = attr2
        ...

# सभी attributes बन जाते हैं obj = MyClass(val1, val2)
  • सभी attributes जानना आसान
  • ऑब्जेक्ट बनते ही attributes बनते हैं
  • कोड अधिक usable और maintainable
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Best practices

1. __init__() में attributes initialize करें
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Best practices

1. __init__() में attributes initialize करें
2. Naming

क्लास के लिए CamelCase, functions और attributes के लिए lower_snake_case

Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Best practices

1. __init__() में attributes initialize करें
2. Naming

क्लास के लिए CamelCase, functions और attributes के लिए lower_snake_case

3. self को self ही रखें
class MyClass:
    # यह काम करता है, पर अनुशंसित नहीं
    def my_method(kitty, attr):
       kitty.attr = attr
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Best practices

1. __init__() में attributes initialize करें
2. Naming

क्लास के लिए CamelCase, functions और attributes के लिए lower_snake_case

3. self वही self
4. Docstrings का उपयोग करें
class MyClass:
    """यह क्लास कुछ नहीं करती"""
    pass
Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

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

Python में ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग

Preparing Video For Download...