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

Python में Object-Oriented Programming परिचय

George Boorman

Curriculum Manager, DataCamp

Methods और attributes

  • Methods क्लास के अंदर function definitions होते हैं
  • पहला argument self होता है
  • assignment से attributes बनाएँ
  • क्लास में attributes को self.___ से refer करें
  • बहुत सारे methods कॉल करना अस्थिर हो सकता है!
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 में Object-Oriented Programming परिचय

Constructor

  • ऑब्जेक्ट बनाते समय data जोड़ें
  • Constructor __init__() method हर बार ऑब्जेक्ट बनने पर कॉल होता है
    • __methodname__ सिंटैक्स के कारण ऑटोमैटिक कॉल होता है
class Customer:
    def __init__(self, name):
        # .name attribute बनाएँ और इसे name पैरामीटर दें
        self.name = name
        print("The __init__ method was called")
Python में Object-Oriented Programming परिचय

Constructor

# __init__ implicitly कॉल होता है 
cust = Customer("Lara de Silva")   
print(cust.name)
The __init__ method was called
Lara de Silva
Python में Object-Oriented Programming परिचय

Methods में attributes

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

    def my_method2(self, attr2):        
        self.attr2 = attr2
        ...

obj = MyClass() # attr1 बना obj.my_method1(val1) # attr2 बना obj.my_method2(val2)

Constructor में attributes

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

# सभी attributes बनते हैं obj = MyClass(val1, val2)
  • आम तौर पर constructor इस्तेमाल करें
  • ऑब्जेक्ट बनते समय attributes बनते हैं
  • कोड अधिक usable और maintainable
Python में Object-Oriented Programming परिचय

Arguments जोड़ें

class Customer:
    # balance argument जोड़ें
    def __init__(self, name, balance): 

self.name = name # balance attribute जोड़ें self.balance = balance print("The __init__ method was called")
Python में Object-Oriented Programming परिचय

Parameters जोड़ें

# __init__ कॉल होता है
cust = Customer("Lara de Silva", 1000)
print(cust.name)
print(cust.balance)
The __init__ method was called
Lara de Silva
1000
Python में Object-Oriented Programming परिचय

Default arguments

class Customer:
    # balance के लिए default value सेट करें
    def __init__(self, name, balance=0):
        self.name = name
        # नया attribute असाइन करें
        self.balance = balance
        print("The __init__ method was called")
Python में Object-Oriented Programming परिचय

Default arguments

# balance को explicitly न दें
cust = Customer("Lara de Silva")

print(cust.name) # balance attribute फिर भी बनता है print(cust.balance)
The __init__ method was called
Lara de Silva
0
Python में Object-Oriented Programming परिचय

Best practices

1. __init__() में attributes initialize करें
Python में Object-Oriented Programming परिचय

Best practices

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

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

Python में Object-Oriented Programming परिचय

Best practices

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

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

3. self को self ही रखें
class MyClass:
    # यह चलता है पर recommend नहीं है
    def my_method(george, attr):
        george.attr = attr
Python में Object-Oriented Programming परिचय

Best practices

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

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

3. self है self
4. Docstrings इस्तेमाल करें
class MyClass:
    """यह क्लास कुछ नहीं करती"""
    pass
Python में Object-Oriented Programming परिचय

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

Python में Object-Oriented Programming परिचय

Preparing Video For Download...