#TIL : Setter and getter behavior of class property in Python3

In previous TIL, I learned about the way to define getter and setter in Javascript

Today, I learned it in Pythonic way ;) (in Python3)

So here is example (the easy way to learn from a code) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class A:
def __init__(self,x):
print ('init', x)
self.x = x

@property
def x(self):
print ('getter')
return self.__x

@x.setter
def x(self, x):
print ('setter', x)
if x < 0:
self.__x = 0
elif x % 2 == 0:
self.__x = x
else:
self.__x = x * 2

a = A(10)
print(a.x)
a.x = -1
print(a.x)
a.x = 2
print(a.x)
a.x = 7
print(a.x)

Output :

1
2
3
4
5
6
7
8
9
10
11
12
13
init 10
setter 10
getter
10
setter -1
getter
0
setter 2
getter
2
setter 7
getter
14

This is so cool feature, and even it has deleter property method, which triggers when we run del object.property :D

In reactive JS framework that they use getter and setter like a core of Reactive, let find out more next articles ;)