In Python, get/set methods are used to access and modify the properties of a class. They provide a way to encapsulate the class variables (also known as class properties) and make them private, so they can only be accessed through these methods.
In Python 3.10, you can use the @property and @ decorators to define get/set methods.
Example code:
class Car:
def __init__(self, make, model, year, price):
self._make = make
self._model = model
self._year = year
self._price = price
@property
def make(self):
return self._make
@make.setter
def make(self, make):
self._make = make
@property
def model(self):
return self._model
@model.setter
def model(self, model):
self._model = model
@property
def year(self):
return self._year
@year.setter
def year(self, year):
self._year = year
@property
def price(self):
return self._price
@price.setter
def price(self, price):
self._price = price
In the example above, we create a Car class with four properties - make, model, year, and price. To define get/set methods for each of these properties, we use the @property and @ decorators.
The @property decorator creates a read-only property that returns the value of the private variable. We define a setter method for each property using the decorator, which allows us to modify the value of the private variable when the property is assigned a new value.
With this implementation, we can create a new car and get/set the values of its properties like this:
my_car = Car('Ford', 'Mustang', 2022, 35000)
# Get the make
make = my_car.make
print(make) # Output: 'Ford'
# Set the make
my_car.make = 'Chevrolet'
print(my_car.make) # Output: 'Chevrolet'
In summary, get/set methods provide an elegant way to access and modify class properties in Python. By using the @property and @ decorators, we can encapsulate properties and make them private, while still allowing external access through the methods.