The most detailed use of property in python

  • 2021-10-16 02:21:20
  • OfStack

Why did you write this article

In fact, because recently learned python property decorator related knowledge, just started to learn in the clouds, as a result, read a lot of related blogs, unfortunately, probably their foundation is not very good, really feel that many people write too abstruse, and not very comprehensive. So I spent a whole afternoon experimenting, and now I share my knowledge about property. If there are any mistakes, I hope you will not hesitate to comment!

What is an property decorator

As the name implies, this is a decorator, which plays an auxiliary role. Please see the following example for specific understanding. As we know, there are many variables in the program that are limited in scope, such as age, salary, height, etc., which cannot be negative. However, when users input, sometimes it is inevitable to input illegal values. If our program does not have a judgment, it is very likely to cause the whole program to crash! Therefore, we often write some functions such as set () and get () to judge, but it is often too troublesome. At this time, property decorator appears, which has the function of judging and is very concise.

Use of set () and get ()


class A:
  def set_age(self, age):
    if 0 < age < 120:
      self.age = age
    else:
      print(" Illegal age ! Default to 18")
      self.age = 0
  def get_age(self):
    return self.age
a = A()
a.set_age(19)
print(a.age)
a.set_age(180)
print(a.age)

The results are as follows:

19
Illegal age! Default is 18
0

It is undeniable that this does achieve the desired effect. In fact, Java language is implemented in this way. But python has a more concise expression, that is, property decorator.

Use of property Decorator


class A:
  @property
  def age(self):
    return self._age
  @age.setter
  def age(self, age):
    if 0 < age < 120:
      self._age = age
    else:
      self._age = 18
      print(" Illegal age ! Default to 18")
a = A()
a.age = 19
print(a.age)
a.age = 180
print(a.age)

Here @ property is equivalent to the get () method, and @ age. setter is equivalent to the set () method.

Benefits

1. The property decorator makes the set () and get () methods properties! You can use. age to set the value without parentheses, which is convenient for change.
2. Added a new feature-read-only. If you only write @ property instead of @ age. setter, then this variable is read-only, and an error will be reported if you re-assign it. The security of the program is increased.
3. Note that property is decorated with private members, which are relatively safer in the first place, which reflects the uniformity of the code.

Matters needing attention

1. If you want to make the decorated member read-only, you must set the value at the time of definition, otherwise an error will be reported.
2. The property decorator can only decorate private members. If it decorates non-private members, it will report a loop error. The result is indeed like this, but the reason is not clear. Please feel free to give advice!
3. The variables before setter must be property modified variables, such as age in this example, and 2 must be 1.


Related articles: