This repository has been archived by the owner on Sep 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr.py
75 lines (50 loc) · 1.49 KB
/
attr.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python3
class testAttr(object):
"""docstring for testAttr"""
def __init__(self, name, age):
super(testAttr, self).__init__()
self._name = name
self.age = age
def get_name(self):
return self._name
def set_name(self, name):
# self._name = name
self.__dict__['_name'] = name
name = property(get_name, set_name)
def __setattr__(self, name, value):
if name == 'info':
self._name, self.age = value
else:
self.__dict__[name] = value #动态的给类增加录属性。 __dict__是保存类全部属性的字典
def __getattr__(self, name):
if name == 'info':
return (self.age, self._name)
else:
raise AttributeError
def show_attr_dict(self):
"""
show all this class attr.
"""
print(self.__dict__) #__dict__是保存类全部属性的字典
tom = testAttr('tom', 22)
print(tom)
tom.info = ('tom2', 23)
print(tom.name, tom.age)
tom.some = "some" ##动态的给类增加录属性。
print(tom.some)
tom.show_attr_dict()
tom.name = 'tom3'
print(tom.name)
def pro(name):
def get_name(instance):
return instance.__dict__[name]
def set_name(instance, value):
instance.__dict__[name] = value
return property(get_name, set_name)
class Test:
def __init__(self, arg):
self.arg = arg
name = pro('name')
t = Test('hello')
t.name = 'kx'
print(t.name)