socketref,再见!高德

https://github.com/adoggie

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  246 Posts :: 4 Stories :: 312 Comments :: 0 Trackbacks

常用链接

留言簿(54)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜


python内部数据容器有dict和list两种 ,工作中最常用的方式是定义一些数据结构(数据即代码),例如: 

1 frog={'name':'scott',
2           'age':2,
3           'parts':{
4                     'eye':'green','leg':85
5                     },
6           ''friend":['moee','wee'],
7           "hometown":'affica'
8          }

frog对象定义了小动物的某些属性,访问frog对象属性时,通过 dict,list的取值方法进行访问,例如:  
print frog['name']
print frog['friend'][0]  
有时觉得这种表达方式太原始了,需要改进,最好是以 '.'方式访问对象属性,例如:  
print frog.name
代码来开始和结束吧, class _x 
 1 class _x:
 2     """
 3     从简单数据类型转换成python对象
 4 
 5     p = _x({'name':'boob','body':{'color':'black'},'toys':[1,2,3,],'age':100})
 6     print p['toys'][1]
 7     print len(p.toys)
 8     print p.body.colors
 9     """
10     def __init__(self,primitive):
11         self.data = primitive
12 
13     def __getattr__(self, item):
14         value = self.data.get(item,None)
15         if type(value) == dict:
16             value = _x(value)
17         return value
18 
19     def __len__(self):
20         return len(self.data)
21 
22     def __str__(self):
23         return str(self.data)
24 
25     def __getitem__(self, item):
26         value = None
27         if type(self.data) in (list,tuple):
28             value = self.data[item]
29             if type(value) in (dict,list,tuple):
30                 value = _x(value)
31         elif type(self.data) == dict:
32             value = self.__getattr__(item)
33         return value







posted on 2015-05-26 14:44 放屁阿狗 阅读(606) 评论(0)  编辑 收藏 引用 所属分类: perl/python/php/lua/tcl

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理