Getter and Setter in Python

Getter and Setter in Python

Getters and setters in Python are different from those in other OOPs languages. The primary use of getters and setters is to ensure data encapsulation in object-oriented programs. In contrast to other object-oriented languages, private variables in Python are not hidden fields. Some OOPs languages use getters and setter’s methods for data encapsulation. We want to conceal an object class’s attributes from other classes so that methods in other classes don’t accidentally modify data.

In OOPs languages, getters and setters are used to retrieve and update data. A getter retrieves an object’s current attribute value, whereas a setter changes an object’s attribute value. In this article, we are going to discuss the getter and setter in Python with examples.

What is Getter in Python?

Getters are the methods that are used in Object-Oriented Programming (OOPS) to access a class’s private attributes. The setattr() function in Python corresponds to the getattr() function in Python. It alters an object’s attribute values.

What is Setter in Python?

The setter is a method that is used to set the property’s value. It is very useful in object-oriented programming to set the value of private attributes in a class.

Generally, getters and setters are mainly used to ensure the data encapsulation in OOPs.

Using the normal function to achieve getters and setter behavior:

There will be no special implementation if we specify normal get() and set() methods to achieve the getters & setters property.

Example:

Let’s take an example to understand how we may use the normal function to achieve the getters and setter function.

Output: After executing this above code, we will get the output as shown below:

The output is:

19
19

Explanation:

To achieve such functionality, the functions get age() and set age() in the above code operate as standard functions do not affect getters and setters. Python has a unique function property ().

Using the property() function to achieve getters and setter behavior

In Python, property() is a built-in function for creating and returning a property object. There are three methods on a property object: getter(), setter(), and delete(). In Python, the property() function takes four arguments: property(fget, fset, fdel, doc). The fget function is used for retrieving the attribute value. The fset function is used for setting the value of an attribute. The fdel function is used to delete the value of an attribute. The attribute is given a docstring by doc.

Example:

Let’s take an example to understand how we can use the property() function to achieve getters and setters behavior.

Output: After executing this above code, we will get the output as shown below:

The output is:

setter method
getter method
18

Explanation:

In the above program, there is only single print statement. The output consists of three lines due to the setter method set age() being called, and the getter method get age() being called. As a result, age is a property object that aids in the security of private variable access.

Using the @property decorators to achieve getters and setter behavior:

We used the property() function in the previous method to achieve getters and setters behavior. However, as previously mentioned in this article, getters and setters are also used to validate the getting and setting of attribute values. Another method for enforcing property function is to use a decorator. @property is one of Python’s built-in decorators. The primary goal of any decorator is to modify the class methods or attributes so that the class user does not need to change their code.

Example:

Let’s take an example to understand how we can use the @property decorators to achieve the getters and setter behavior.

Output: After executing the above code, we will get the output as shown below:

The output is:

setter method called
getter method called
25

Explanation:

The preceding code demonstrates how to use the @property decorator to build pythonic getters and setters. The above example serves as validation code, raising a ValueError if we attempt to initialize age with a value less than 20.

Making the attributes private

Now, we’ll see how to render the methods private so that external calling functions can’t manipulate the variables within them. They could only be manipulated by functions that are defined within the class. They are made private by prefixing them with two underscores.

Example:

Let’s take an example to understand how we can make the attributes private in the program.

Output: After executing this above code, we will get the output as shown below:

49

Reading the values from private methods

To read the value from private methods, we have to use the getter method. Without using the getter method, we can’t use the property method to access the private attribute values. Let’s take an example to understand this method.

Example:

Output: After executing this above code, we will get the output as shown below:

The output is:

27
2020

Example:

Let’s take another example to create a class with properties. From that class we may several objects.

The property (Emp) is not set on these objects. We could set it directly, but that is not a good method. We construct two methods instead: getEmp() and setEmp().

Output: After executing this above code, we will get the output as shown below:

The output is:

Designer
Developer

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263515.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论