What are Getters and Setters?
- Getters: These are the methods used in Object-Oriented Programming (OOPS) which helps to access the private attributes from a class.
- Setters: These are the methods used in OOPS feature which helps to set the value to private attributes in a class.
Private Attribute - Encapsulation
If you are not familiar with the private attributes or private methods in Python, read this DataCamp article.
Let's see how you can implement a private attribute in Python.
class SampleClass:
def __init__(self, a):
self.__a = a
def get_a(self):
return self.__a
def set_a(self, a):
self.__a = a
SampleClass has three methods.
- get_a:- It is used to get the values of private attribute a.
- set_a:- It is used to set the value of a using an object of a class.
You are not able to access the private variables directly in Python. That's why you implemented the getter method.
Let's see how the class works.
obj = SampleClass(10)
print(obj.get_a())
obj.set_a(45)
print(obj.get_a())
This is how you implement private attributes, getters, and setters in Python. The same process was followed in Java. Let's write the same implementation in a Pythonic way.
class PythonicWay:
def __init__(self, a):
self.a = a
You don't need any getters, setters methods to access or change the attributes. You can access it directly using the name of the attributes.
Let's see.
obj = PythonicWay(100)
print(obj.a)
What's the difference between the above two classes.
- SampleClass hides the private attributes and methods. It implements the encapsulation feature of OOPS.
- PythonicWay doesn't hide the data. It doesn't implement any encapsulation feature.
What's the better to use?
- This depends on our need. If you want private attributes and methods you can implement the class using setters, getters methods otherwise you will implement using the normal way.
Property
Now, what if you want to have some conditions to set the value of an attribute in the SampleClass.
Let's say if the value we passed is even and positive then we can set it to the attribute, otherwise set the value to 2.
Let's implement this by changing the set_a() method in the SampleClass.
class SampleClass1:
def __init__(self, a):
self.set_a(a)
def get_a(self):
return self.__a
def set_a(self, a):
if a > 0 and a % 2 == 0:
self.__a = a
else:
self.__a = 2
Let's check the class by creating an object.
obj = SampleClass1(16)
print(obj.get_a())
Let's see how to implement the above class using the @property decorator.
class Property:
def __init__(self, var):
self.a = var
@property
def a(self):
return self.__a
@a.setter
def a(self, var):
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
@property is used to get the value of a private attribute without using any getter methods. We have to put a line @property in front of the method where we return the private variable.
To set the value of the private variable, we use @method_name.setter in front of the method. We have to use it as a setter.
Let's test the class Property to check whether the decorators is working properly or not.
obj = Property(23)
print(obj.a)
@a.setter will set the value of a by checking the conditions we have mentioned in the method. Another way to use the property is...
class AnotherWay:
def __init__(self, var):
self.set_a(var)
def get_a(self):
return self.__a
def set_a
(self, var):
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
a = property(get_a, set_a)
Pass all the getter and setter methods to the property and assign it to the variable which you have to use as a class attribute.
obj = AnotherWay(28)
print(obj.a)
Make the setter and getter methods as private to hide them. Let's do it.
class FinalClass:
def __init__(self, var):
self.__set_a(var)
def __get_a(self):
return self.__a
def __set_a(self, var):
if var > 0 and var % 2 == 0:
self.__a = var
else:
self.__a = 2
a = property(__get_a, __set_a)
Let's check whether it's working properly or not.
obj = FinalClass(12)
print(obj.a)
Conclusion
You have now seen how you can implement encapsulation in Python and the difference between the setter, getter and, property.
If you are new to Python, I recommend that you take DataCamp's free Introduction to Python course.
If you have any questions regarding the article, please mention them in the comment section. Happy Coding!
What are Getters and Setters?
Private Attribute - Encapsulation
If you are not familiar with the private attributes or private methods in Python, read this DataCamp article.
Let's see how you can implement a private attribute in Python.
SampleClass has three methods.
__init__:- It is used to initialize the attributes or properties of a class.
You are not able to access the private variables directly in Python. That's why you implemented the getter method.
Let's see how the class works.
This is how you implement private attributes, getters, and setters in Python. The same process was followed in Java. Let's write the same implementation in a Pythonic way.
You don't need any getters, setters methods to access or change the attributes. You can access it directly using the name of the attributes.
Let's see.
What's the difference between the above two classes.
What's the better to use?
Property
Now, what if you want to have some conditions to set the value of an attribute in the SampleClass.
Let's say if the value we passed is even and positive then we can set it to the attribute, otherwise set the value to 2.
Let's implement this by changing the set_a() method in the SampleClass.
Let's check the class by creating an object.
Let's see how to implement the above class using the @property decorator.
@property is used to get the value of a private attribute without using any getter methods. We have to put a line @property in front of the method where we return the private variable.
To set the value of the private variable, we use @method_name.setter in front of the method. We have to use it as a setter.
Let's test the class Property to check whether the decorators is working properly or not.
@a.setter will set the value of a by checking the conditions we have mentioned in the method. Another way to use the property is...
Pass all the getter and setter methods to the property and assign it to the variable which you have to use as a class attribute.
Make the setter and getter methods as private to hide them. Let's do it.
Let's check whether it's working properly or not.
Conclusion
You have now seen how you can implement encapsulation in Python and the difference between the setter, getter and, property.
If you are new to Python, I recommend that you take DataCamp's free Introduction to Python course.
If you have any questions regarding the article, please mention them in the comment section. Happy Coding!