python自定义字典的扩展类,让字典操作起来更容易,python自定义,类定义如下class e
python自定义字典的扩展类,让字典操作起来更容易,python自定义,类定义如下class e
类定义如下
class easyaccessdict(dict): def __getattr__(self,name): if name in self: return self[name] n=easyaccessdict() super().__setitem__(name, n) return n def __getitem__(self,name): if name not in self: super().__setitem__(name,nicedict()) return super().__getitem__(name) def __setattr__(self,name,value): super().__setitem__(name,value)
使用范例
>>> d= easyaccessdict()>>> d{}>>> d.foo.bar= 'a'>>> d{'foo':{'bar':'a'}}>>> d['foo']{'bar':'a'}>>> d['foo'].blah= 7>>> d{'foo':{'bar':'a', 'blah':7}}>>> d.a.b.c.e.e.f.g.h= 11
相关内容
- 批量剪切图片 - Python,,import osfro
- Kindeditor+web.py+SAE Storage 实现文件上传,kindeditorweb.py,#上
- python mysql insert delete query,pythonmysql,python连接mysq
- ergodic_db_tb.py,,遍历数据库表及其相关表操
- 如何获得python对象变量的类型名字,python对象变量类型
- python从url中获取文件名,,import urlli
- 在Django中使用group_by,django使用group_by,在Django中怎样使
- python 显示错误的堆栈信息,python堆栈,python中有专门处
- 找零时纸币/硬币数量最少的算法,找零纸币硬币算法
- 计算未来某个时间点到现在还有多久,未来到现在,im
评论关闭