1class Number:
2
3 def __init__(self, number):
4 self.__number = number
5
6 def get_number(self):
7 print("Getting...")
8 return self.__number
9
10 def set_number(self, number):
11 print("Setting...")
12 self.__number = number
13
14 def del_number(self):
15 print("Deleting...")
16 del self.__number
17
18 nb = property()
19 nb = nb.getter(get_number)
20 nb = nb.setter(set_number)
21 nb = nb.deleter(del_number)
22
23n = Number(42)
24
25print(n.nb)
26n.nb = 69
27print(n.nb)
28del n.nb
29
30# Getting...
31# 42
32# Setting...
33# Getting...
34# 69
35# Deleting...
1class Person:
2 def __init__(self, name):
3 self._name = name
4
5 @property
6 def name(self):
7 print('Getting name')
8 return self._name
9
10 @name.setter
11 def name(self, value):
12 print('Setting name to ' + value)
13 self._name = value
14
15 @name.deleter
16 def name(self):
17 print('Deleting name')
18 del self._name
19
20p = Person('Adam')
21print('The name is:', p.name)
22p.name = 'John'
23del p.name
1class Person:
2 def __init__(self, name):
3 self._name = name
4
5 def get_name(self):
6 print('Getting name')
7 return self._name
8
9 def set_name(self, value):
10 print('Setting name to ' + value)
11 self._name = value
12
13 def del_name(self):
14 print('Deleting name')
15 del self._name
16
17 # Set property to use get_name, set_name
18 # and del_name methods
19 name = property(get_name, set_name, del_name, 'Name property')
20
21p = Person('Adam')
22print(p.name)
23p.name = 'John'
24del p.name