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

@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:

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).

Code Description
In the provided Python code, the @property.setter decorator is used to create a setter method for a property defined using the @property decorator. This allows you to provide custom behavior when assigning a value to the property, effectively controlling how the attribute's value is modified.

In the code, we have a class called User with a constructor method __init__ that takes two parameters, username and password, and initializes two instance attributes: username and __password.

The interesting part is the password property, which is created using the @property decorator. The password property is a read-only property, as it only has a getter method but no setter. The getter method, defined below the @property decorator, returns the value of the private attribute __password.

To make the password property updatable, the @password.setter decorator is used to define a setter method for the property. The setter method, also named password, takes a new_password parameter. When you assign a new value to the password property (e.g., bob.password = "new_bob_password_123"), the setter method is automatically called with the assigned value. Inside the setter method, the value of the private attribute __password is updated with the provided new_password.

In the usage part of the code, an instance of the User class is created, named bob, with the username "bob123top" and the password "bob123456". Initially, the password property is accessed using bob.password, and it returns the value "bob123456", which is the initial password.

Next, the password property is updated by assigning a new value to it using bob.password = "new_bob_password_123". This assignment calls the setter method defined by @password.setter, and the __password attribute is updated with the new value "new_bob_password_123".

Finally, the password property is accessed again using bob.password, and it returns the updated value "new_bob_password_123".

In summary, the @property.setter decorator allows you to define a setter method for a property, which controls how the attribute's value is modified when you assign a new value to the property. It complements the @property decorator, enabling you to create properties that provide custom behavior for both reading and updating attribute values.

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.

Code Description
Updated password setter includes some validation checks for the new password:
  1. It first checks if the new_password is an instance of the str class using the isinstance() function.
  2. If the new_password is indeed a string, the code proceeds to check the length of the password using len(new_password). If the length is greater than or equal to 8 characters, the new_password is considered valid, and it assigns the value to the self.__password attribute. The use of double underscores (__password) indicates that the password attribute is intended to be private or inaccessible outside the class.
  3. If the length of new_password is less than 8 characters, the code prints a message indicating that the password length must be greater than or equal to 8.
  4. If the new_password is not a string (i.e., it fails the isinstance() check), the code prints a message stating that the password must be of type string.

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?

Select a few correct answers

Everything was clear?

Section 3. Chapter 8
course content

Course Content

In-Depth Python OOP

@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:

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).

Code Description
In the provided Python code, the @property.setter decorator is used to create a setter method for a property defined using the @property decorator. This allows you to provide custom behavior when assigning a value to the property, effectively controlling how the attribute's value is modified.

In the code, we have a class called User with a constructor method __init__ that takes two parameters, username and password, and initializes two instance attributes: username and __password.

The interesting part is the password property, which is created using the @property decorator. The password property is a read-only property, as it only has a getter method but no setter. The getter method, defined below the @property decorator, returns the value of the private attribute __password.

To make the password property updatable, the @password.setter decorator is used to define a setter method for the property. The setter method, also named password, takes a new_password parameter. When you assign a new value to the password property (e.g., bob.password = "new_bob_password_123"), the setter method is automatically called with the assigned value. Inside the setter method, the value of the private attribute __password is updated with the provided new_password.

In the usage part of the code, an instance of the User class is created, named bob, with the username "bob123top" and the password "bob123456". Initially, the password property is accessed using bob.password, and it returns the value "bob123456", which is the initial password.

Next, the password property is updated by assigning a new value to it using bob.password = "new_bob_password_123". This assignment calls the setter method defined by @password.setter, and the __password attribute is updated with the new value "new_bob_password_123".

Finally, the password property is accessed again using bob.password, and it returns the updated value "new_bob_password_123".

In summary, the @property.setter decorator allows you to define a setter method for a property, which controls how the attribute's value is modified when you assign a new value to the property. It complements the @property decorator, enabling you to create properties that provide custom behavior for both reading and updating attribute values.

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.

Code Description
Updated password setter includes some validation checks for the new password:
  1. It first checks if the new_password is an instance of the str class using the isinstance() function.
  2. If the new_password is indeed a string, the code proceeds to check the length of the password using len(new_password). If the length is greater than or equal to 8 characters, the new_password is considered valid, and it assigns the value to the self.__password attribute. The use of double underscores (__password) indicates that the password attribute is intended to be private or inaccessible outside the class.
  3. If the length of new_password is less than 8 characters, the code prints a message indicating that the password length must be greater than or equal to 8.
  4. If the new_password is not a string (i.e., it fails the isinstance() check), the code prints a message stating that the password must be of type string.

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?

Select a few correct answers

Everything was clear?

Section 3. Chapter 8
some-alt