Attribute Access को कस्टमाइज़ करना

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

AttributeError

class Student:
  def __init__(self, student_name, major):
    self.student_name = student_name
    self.major = major
...
karina = Student("Karina", "Literature")
student.residence_hall  # ऐसा attribute एक्सेस करने की कोशिश जो मौजूद नहीं है
...
AttributeError: 'Student' object has no attribute 'residence_hall'
Intermediate Object-Oriented Programming in Python

__getattr__()

# Rest of the class definition above

  def __getattr__(self, name):
    # Implement logic here
    ...

किसी ऑब्जेक्ट का namespace उस ऑब्जेक्ट से जुड़े attributes का संग्रह होता है

__getattr__() तब चलता है जब किसी ऑब्जेक्ट के namespace के बाहर के किसी भी attribute को एक्सेस करने की कोशिश की जाती है

  • मैजिक मेथड, सीधे कॉल नहीं किया जाता
  • name पैरामीटर लेता है
  • AttributeError उठाने की जगह कस्टम फ़ंक्शनैलिटी लागू करता है
Intermediate Object-Oriented Programming in Python

AttributeError को हल करना

class Student:
  def __init__(self, student_name, major):
    self.student_name = student_name
    self.major = major

  def __getattr__(self, name):
      print(f"""{name} does not exist in this object's namespace, try setting 
            a value for {name} first""")
karina.residence_hall  # अब, residence_hall attribute फिर से पाने की कोशिश करें
residence_hall does not exist in this object's namespace, try setting a value for 
  residence_hall first
Intermediate Object-Oriented Programming in Python

__setattr__()

__setattr__() एक मैजिक मेथड है जो (नए या मौजूदा) attribute के set या update होने पर चलता है

  • __init__() से सेट किए गए attributes भी शामिल हैं
  • attribute का name और value लेता है
  • ऑब्जेक्ट के __dict__ attribute का उपयोग करता है

$$

$$

attributes में बदलाव नियंत्रित करना, वैलिडेशन, ट्रांसफॉर्मेशन

# Rest of the class definition above

  def __setattr__(self, name, value):
    # Implement logic here
    ...

    # Use __dict__ to create/update 
    # the attribute
    self.__dict__[name] = value

__dict__ ऑब्जेक्ट के सभी attributes स्टोर करता है, डेटा पाने और स्टोर करने में काम आता है

Intermediate Object-Oriented Programming in Python

Attribute स्टोरेज को कस्टमाइज़ करना

class Student:
  def __init__(self, student_name, major):
    self.student_name = student_name
    self.major = major

  def __setattr__(self, name, value):
    # If value is a string, set the attribute using the __dict__ attribute
    if isinstance(value, str):
      print(f"Setting {name} = {value}")
      self.__dict__[name] = value

    else: # Otherwise, raise an exception noting an incorrect data type
        raise Exception("Unexpected data type!")
Intermediate Object-Oriented Programming in Python

कार्रवाई में __setattr__

# 'str' टाइप की वैल्यू से attribute सेट करें
karina.residence_hall = "Honors College South"
print(karina.residence_hall)
Setting residence_hall = Honors College South
Honors College South
# 'int' टाइप की वैल्यू से attribute सेट करें
karina.student_id = 19301872
...
    raise Exception("Unexpected data type!")
Exception: Unexpected data type!
Intermediate Object-Oriented Programming in Python

__getattr__ और __setattr__ साथ में

class Student:
    ...   

    def __getattr__(self, name):
        # Set the attribute with a placeholder
        self.__setattr__(name, None)
        return None

    def __setattr__(self, name, value):
        if value is None:  # Print a message denoting a placeholder
            print(f"Setting placeholder for {name}")

        self.__dict__[name] = value  # Set the attribute
Intermediate Object-Oriented Programming in Python

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

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...