कोर्स
Getters और Setters क्या हैं?
- Getters: ये Object-Oriented Programming (OOPS) में उपयोग होने वाले वे मेथड हैं जो किसी class के private attributes तक पहुँच बनाने में मदद करते हैं।
- Setters: ये OOPS की वह सुविधा है जिसके मेथड किसी class के private attributes के मान सेट करने में मदद करते हैं।
Private Attribute - Encapsulation
यदि आप Python में private attributes या private methods से परिचित नहीं हैं, तो यह DataCamp लेख पढ़ें।
आइए देखें कि आप Python में private attribute कैसे लागू कर सकते हैं।
class SampleClass:
def __init__(self, a):
## private varibale or property in Python
self.__a = a
## getter method to get the properties using an object
def get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def set_a(self, a):
self.__a = a
SampleClass में तीन मेथड हैं।
-
__init__:- यह किसी class के attributes या properties को initialize करने के लिए उपयोग होता है।
- __a:- यह एक private attribute है।
- get_a:- यह private attribute a के मान प्राप्त करने के लिए उपयोग होता है।
- set_a:- यह object के माध्यम से a का मान सेट करने के लिए उपयोग होता है।
Python में आप private variables को सीधे एक्सेस नहीं कर सकते। इसी कारण आपने getter मेथड लागू किया है।
देखते हैं यह class कैसे काम करती है।
## creating an object
obj = SampleClass(10)
## getting the value of 'a' using get_a() method
print(obj.get_a())
## setting a new value to the 'a' using set_a() method
obj.set_a(45)
print(obj.get_a())
10
45
यही तरह आप Python में private attributes, getters, और setters लागू करते हैं। यही प्रक्रिया Java में भी अपनाई जाती थी। आइए उसी को Pythonic तरीके से लिखें।
class PythonicWay:
def __init__(self, a):
self.a = a
Attributes को एक्सेस या बदलने के लिए आपको किसी getter, setter मेथड की ज़रूरत नहीं। आप सीधे attribute के नाम से इन्हें एक्सेस कर सकते हैं।
आइए देखें।
## Creating an object for the 'PythonicWay' class
obj = PythonicWay(100)
print(obj.a)
100
ऊपर दी गई दोनों classes में क्या अंतर है?
- SampleClass private attributes और methods को छिपाती है। यह OOPS की encapsulation सुविधा को लागू करती है।
- PythonicWay डेटा नहीं छिपाती। यह किसी भी प्रकार की encapsulation सुविधा लागू नहीं करती।
कौन-सा उपयोग करना बेहतर है?
- यह आपकी आवश्यकता पर निर्भर करता है। यदि आपको private attributes और methods चाहिए हों, तो आप class को setters, getters मेथड्स के साथ लागू कर सकते हैं, अन्यथा सामान्य तरीके से लागू करेंगे।
Property
अब मान लें कि आप SampleClass में किसी attribute का मान सेट करने से पहले कुछ शर्तें लागू करना चाहते हैं।
मान लीजिए यदि दिया गया मान सम और धनात्मक है तो उसे attribute में सेट करें, अन्यथा मान 2 सेट करें।
आइए यह SampleClass में set_a() मेथड बदलकर लागू करें।
class SampleClass1:
def __init__(self, a):
## calling the set_a() method to set the value 'a' by checking certain conditions
self.set_a(a)
## getter method to get the properties using an object
def get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def set_a(self, a):
## condition to check whether 'a' is suitable or not
if a > 0 and a % 2 == 0:
self.__a = a
else:
self.__a = 2
आइए class को एक object बनाकर परखें।
## creating an object for the class 'SampleClass1'
obj = SampleClass1(16)
print(obj.get_a())
16
आइए ऊपर दी गई class को @property decorator का उपयोग करते हुए लागू करना देखें।
class Property:
def __init__(self, var):
## initializing the attribute
self.a = var
@property
def a(self):
return self.__a
## the attribute name and the method name must be same which is used to set the value for the attribute
@a.setter
def a(self, var):
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
@property का उपयोग किसी private attribute का मान बिना किसी getter मेथड के पाने के लिए होता है। जिस मेथड में हम private variable लौटाते हैं, उसके आगे हमें @property लिखना होता है।
Private variable का मान सेट करने के लिए हम मेथड के आगे @method_name.setter का उपयोग करते हैं। इसे setter के रूप में ही इस्तेमाल करना होता है।
आइए Property class को परखें कि decorators सही तरह काम कर रहे हैं या नहीं।
## creating an object for the class 'Property'
obj = Property(23)
print(obj.a)
2
@a.setter मेथड में बताई गई शर्तों को जाँचकर a का मान सेट करेगा। property का उपयोग करने का एक और तरीका...
class AnotherWay:
def __init__(self, var):
## calling the set_a() method to set the value 'a' by checking certain conditions
self.set_a(var)
## getter method to get the properties using an object
def get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def set_a
(self, var):
## condition to check whether var is suitable or not
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
a = property(get_a, set_a)
सभी getter और setter मेथड्स को property में पास करें और उसे उस variable को असाइन करें जिसे आप class का attribute की तरह उपयोग करना चाहते हैं।
## creating an object for the 'AnotherWay' class
obj = AnotherWay(28)
print(obj.a)
28
setter और getter मेथड्स को छिपाने के लिए उन्हें private बना दें। आइए करें।
class FinalClass:
def __init__(self, var):
## calling the set_a() method to set the value 'a' by checking certain conditions
self.__set_a(var)
## getter method to get the properties using an object
def __get_a(self):
return self.__a
## setter method to change the value 'a' using an object
def __set_a(self, var):
## condition to check whether var is suitable or not
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
a = property(__get_a, __set_a)
आइए जाँचें कि यह ठीक से काम कर रहा है या नहीं।
## creating an object for the 'AnotherWay' class
obj = FinalClass(12)
print(obj.a)
12
निष्कर्ष
अब आपने देखा कि आप Python में encapsulation कैसे लागू कर सकते हैं और setter, getter तथा property के बीच क्या अंतर है।
यदि आप Python में नए हैं, तो मैं DataCamp का निःशुल्क Introduction to Python कोर्स करने की सलाह देता/देती हूँ।
यदि लेख के बारे में आपके कोई प्रश्न हों, तो कृपया उन्हें टिप्पणी अनुभाग में लिखें। शुभ कोडिंग!