-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperty05.py
More file actions
47 lines (38 loc) · 1.25 KB
/
property05.py
File metadata and controls
47 lines (38 loc) · 1.25 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#copyRight by heibanke
class Car(object):
country = u'中国'
__slots__=('length','width','height','owner','__dict__')
def __init__(self, length, width, height, owner=None):
self.owner = owner
self.length = length
self.width = width
self.height = height
def __getattr__(self,name):
print ("__getattr__",name)
assert name in self.__slots__, "Not have this attribute "+name
return self.__dict__.get(name,None)
def __setattr__(self,name,value):
print ("__setattr__",name)
assert name in self.__slots__, "Not have this attribute "+name
if name!='owner':
assert value>0, name+" must larger than 0"
self.__dict__[name]=value
def __delattr__(self,name):
print ("__delattr__",name)
assert name in self.__slots__, "Not have this attribute "+name
if name=='owner':
self.__dict__[name]=None
if __name__ == '__main__':
a = Car(1.2,1.4,1.5,u'黑板客')
"""
print a.owner
del a.owner
print a.owner
a.length=1
print a.country
a.country = 'china'
print a.country
a.name = u"一汽"
"""