← Back to Python Course | Chapter 7: OOP | Lesson 10 of 11

Python Property Decorators

Property एक plain variable जैसी दिखती है पर इस्तेमाल करते ही चुपके से code चलाती है, जैसे एक दरवाज़ा जिसका guard आपको check-in करता है। यह control करने देती है कि कोई value कैसे पढ़ी या बदली जाए।
Syntax
python
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?

python
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

python
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

python
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

python
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

python
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)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.