summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/property.py
blob: 7f3c833ad3f5fba11e870c87be56acc2ec1cadc8 (plain) (blame)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class A:
    def __init__(self, x):
        self._x = x

    @property
    def x(self):
        print("x get")
        return self._x

a = A(1)
print(a.x)

try:
    a.x = 2
except AttributeError:
    print("AttributeError")

class B:
    def __init__(self, x):
        self._x = x

    def xget(self):
        print("x get")
        return self._x

    def xset(self, value):
        print("x set")
        self._x = value

    x = property(xget, xset)

b = B(3)
print(b.x)
b.x = 4
print(b.x)

class C:
    def __init__(self, x):
        self._x = x

    @property
    def x(self):
        print("x get")
        return self._x

    @x.setter
    def x(self, value):
        print("x set")
        self._x = value

c = C(5)
print(c.x)
c.x = 6
print(c.x)