Python Property Decorators
In this page:
class ClassName:
@property
def attribute(self):
return self._attribute
@attribute.setter
def attribute(self, value):
self._attribute = value
@property क्या है?
@property किसी method को ऐसी चीज़ में बदल देता है जिसे callers बिना parentheses के, एक ordinary attribute की तरह access करते हैं — def full_name(self): return f'{self.first} {self.last}' obj.full_name() की बजाय obj.full_name बन जाता है — जो read-only computed values के लिए ideal है।
उदाहरण: What is @property?
class Person:
def __init__(self, first, last):
self.first = first
self.last = last
@property
def full_name(self): # accessed without parentheses, like an attribute
return f"{self.first} {self.last}"
p = Person("Alex", "Doe")
print(p.full_name)
Property Setter
एक matching @name.setter method जोड़ना उसी attribute को assignment accept करने देता है (obj.balance = 100) जबकि पहले आपका अपना validation code चलता है, जैसे store होने से पहले negative balance को reject करना — कुछ ऐसा जो plain attribute assignment intercept नहीं कर सकता।
उदाहरण: The Property Setter
class Account:
def __init__(self, balance):
self._balance = balance
@property
def balance(self):
return self._balance
@balance.setter
def balance(self, value):
if value < 0:
raise ValueError("Balance cannot be negative") # validation on assignment
self._balance = value
a = Account(100)
a.balance = 200 # runs through the setter
print(a.balance)
Property Deleter
एक @name.deleter method तब custom cleanup logic चलाता है जब कोई 'del obj.balance' लिखता है — उन properties के लिए उपयोगी जो किसी external resource (जैसे एक cached file handle) पर backed हों और जिन्हें सिर्फ गायब होने की बजाय explicit teardown चाहिए।
उदाहरण: The Property Deleter
class Account:
def __init__(self, balance):
self._balance = balance
@property
def balance(self):
return self._balance
@balance.deleter
def balance(self): # runs custom logic on del a.balance
print("Cleaning up balance")
del self._balance
a = Account(100)
del a.balance # triggers the deleter above
Calculated Properties
चूँकि किसी property का getter पर्दे के पीछे बस एक method है, यह हर बार access होने पर अपनी return value ताज़ा compute कर सकता है — एक .full_name property हमेशा current first और last name reflect कर सकती है, अगर वे स्वतंत्र रूप से बदलें तो भी stale हुए बिना।
उदाहरण: Calculated Properties
class Person:
def __init__(self, first, last):
self.first = first
self.last = last
@property
def full_name(self): # recomputed every time it's accessed
return f"{self.first} {self.last}"
p = Person("Alex", "Doe")
p.first = "Sam"
print(p.full_name) # reflects the updated first name
Legacy Code को Refactor करना
Properties आपको बाद में किसी plain public attribute को validated attribute में बदलने देते हैं, बिना obj.balance पढ़ने या लिखने वाले किसी मौजूदा code को तोड़े — callers वही syntax इस्तेमाल करते रहते हैं जबकि आपकी class चुपचाप पर्दे के पीछे validation या computation जोड़ देती है।
उदाहरण: Refactoring Legacy Code
class Account:
def __init__(self, balance):
self._balance = balance
@property
def balance(self):
return self._balance
@balance.setter
def balance(self, value):
if value < 0:
raise ValueError("Invalid balance")
self._balance = value
a = Account(100)
a.balance = 50 # still looks like plain attribute access
print(a.balance)
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: