Python 101 : The "@property" decorator for classes -Setter and Getter methods -


The "@property" refers to a python decorator a function that adds functionalities to another function without modifying its code -.

The "@property" decorator allows us to make a class variable private and define methods to access it, without the need to change any other code that has access to this variable.

For example the code that assigns a value to our variable, does not need to call the setter method, the existing "=" assignment will call the setter without any added code.

Below an example or decorated settergetter and deleter methods:


The "brand" attribute" is protected.

Remark:
  • __var : private variable - two undescores -, not accessible from outside the class
  • _var : protected variable - one underscore - accessible from within the same package

Comments

Leave as a comment:

Archive