Home > Backend Development > Python Tutorial > Accessor and modifier methods in Python

Accessor and modifier methods in Python

WBOY
Release: 2023-08-20 17:33:28
forward
1063 people have browsed it

Accessor and modifier methods in Python

In Python, accessor and modifier methods are used to access private data of a class, which cannot be accessed from outside the class. In object-oriented programming, the data of a class object is encapsulated, that is, the object data is kept as private data and cannot be accessed from outside the object. Provide access to this private data using accessor and modifier methods in Python. These methods are also known as getter and setter methods in Python. In this article, we will understand accessor and modifier methods with examples.

Accessor methods

Accessor methods are used to access object data. An object's private variables can be accessed using accessor methods. Accessor methods are declared as public methods and are used to return private member data of an object. Accessor methods are also called getter methods because they are used to get object data.

In Python the accessor method is defined using @property decorator. When the accessor method is called it returns the private member variable value of the object.

The Chinese translation of

Example

is:

Example

In the following example, we will define a class named Person, which contains a private variable _name. Then, we create an accessor method named name that returns the value of the private member variable _name of the Person class. We can access the value of the _name attribute by creating a person object and using the name accessor method.

class Person:
   def __init__(self, name):
      self.__name = name

   @property
   def name(self):
      return self.__name

person = Person("John")
print(person.name)  
Copy after login

Output

John
Copy after login

Mutator Method

Mutator methods are used to modify an object's private data. Mutator methods are also called setter methods as they are used to set/modify the value of an object private variable. Mutator methods are declared private which modifies the private value of the object variables.

In python mutator methods are defined using the @.setter decorator which specifies that the particular method behaves like a setter method. When the mutator method is called it sets the value of the object private variable.

The Chinese translation of

Example

is:

Example

In the following example, we define a Person class that has a private _name variable. We also define an accessor method named name and a modifier method named name using the @property and @name.setter decorators respectively. When the function is called and passed a value argument, the name modifier method modifies the value of the _name variable.

class Person:
   def __init__(self, name):
      self.__name = name

   @property
   def name(self):
      return self.__name

   @name.setter
   def name(self, value):
      self.__name = value

person = Person("John")
person.name = "Jane"
print(person.name)  
Copy after login

Output

Jane
Copy after login

Conclusion

Accessor and modifier methods are used in object-oriented programming to provide access to the private variables of an object. These methods are also known as getter and setter methods because they are used to get and set/modify the object's private variables respectively. In Python, accessor and modifier methods are defined using the @property and @.setter decorators respectively.

The above is the detailed content of Accessor and modifier methods in Python. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template