-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmydict2.py
More file actions
40 lines (34 loc) · 839 Bytes
/
Copy pathmydict2.py
File metadata and controls
40 lines (34 loc) · 839 Bytes
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
class Dict(dict):
'''
Simple dict but also support access as x.y style.
>>> d1=Dict()
>>> d1['x']=100
>>> d1.x
100
>>> d1.y=200
>>> d1['y']
200
>>> d2=Dict(a=1,b=2,c='3')
>>> d2.c
'3'
>>> d2['empty']
Traceback (most recent call last):
...
KeyError: 'empty'
>>> d2.empty
Traceback (most recent call last):
...
AttributeError: 'Dict' object has no attribute 'empty'
'''
def __init__(self,**kw):
super(Dict,self).__init__(**kw)
def __getattr__(self,key):
try:
return self[key]
except KeyError:
raise AttributeError(r"'Dict' object has no attribute '%s'" % key)
def __setattr__(self,key,value):
self[key]=value
if __name__=="__main__":
import doctest
doctest.testmod()