Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
@property.setter | Encapsulation
In-Depth Python OOP
course content

Course Content

In-Depth Python OOP

In-Depth Python OOP

1. OOP Concepts
2. Inheritance
3. Encapsulation
4. Polymorphism and Abstraction
5. Magic Methods

book@property.setter

Let's go back to the User class with a private attribute password that cannot be changed in subclasses. How can we access the private password? Let's create a property that returns the private password:

1234567891011
class User: def __init__(self, username, password): self.username = username self.__password = password @property def password(self): return self.__password bob = User("bob123top", "bob123456") print(bob.password)
copy

Currently, we can retrieve the password but cannot change it using the regular syntax. To be able to set a value for a property, we need to use the @{property name}.setter decorator, where {property name} is the name of the property we have created (the one for which we want to set a setter).

12345678910111213141516171819
class User: def __init__(self, username, password): self.username = username self.__password = password @property def password(self): return self.__password @password.setter def password(self, new_password): self.__password = new_password bob = User("bob123top", "bob123456") print("(old)", bob.password) bob.password = "new_bob_password_123" print("(new)", bob.password)
copy

The @password.setter decorator indicates that this method should be invoked whenever the password property is assigned a new value. The second password method is decorated by @password.setter (where password is the name of the property method) and set up a new password via assignment (=) operation. The password setter method takes two parameters: self (referring to the instance of the class) and new_password (representing the new value being assigned to the password attribute).

Let's choose the following password restrictions:

  1. The password must be of a type string.
  2. Password length must be greater than 8.

If the new password does not meet the requirements, the corresponding messages will be printed.

1234567891011121314151617181920212223242526272829303132
class User: def __init__(self, username, password): self.username = username self.__password = password @property def password(self): return self.__password @password.setter def password(self, new_password): if isinstance(new_password, str): if len(new_password) >= 8: self.__password = new_password else: print("The password length must be >= 8") else: print("Password must be STRING") bob = User("bob123top", "bob123456") print("(old)", bob.password) bob.password = 122 print("(old)", bob.password) bob.password = "123" print("(old)", bob.password) bob.password = "bob.top.user.123" print("(new)", bob.password)
copy

This code ensures that whenever a new value is assigned to the password attribute, it undergoes validation checks for length and type, providing appropriate feedback if the new password does not meet the requirements.

How to define a property setter?

How to define a property setter?

Select a few correct answers

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 8
some-alt