python使用点操作符访问字典(dict)数据
2013-09-26 08:04
771 查看
平时访问字典使用类似于:dict['name']的方式,如果能通过dict.name的方式访问会更方便,下面的代码自定义了一个类提供了这种方法。
#from http://www.sharejs.com class DottableDict(dict): def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) self.__dict__ = self def allowDotting(self, state=True): if state: self.__dict__ = self else: self.__dict__ = dict() d = DottableDict() d.allowDotting() d.foo = 'bar' print(d['foo']) # bar print(d.foo) # bar d.allowDotting(state=False) print(d['foo']) # bar from http://www.sharejs.com print(d.foo) # AttributeError: 'DottableDict' object has no attribute 'foo' #该代码片段来自于: http://www.sharejs.com/codes/python/8716[/code] 代码转自:脚本分享网 http://www.sharejs.com/codes/python/8716
相关文章推荐
- python使用点操作符访问字典(dict)数据的方法
- python中将普通对象作为 字典类(dict) 使用
- 使用python访问网络上的数据
- Python dict dictionaries Python 数据结构——字典
- Python字典的使用:dict、set的使用
- 详解Python中映射类型(字典)操作符的概念和使用
- Python数据类型详解(四)字典:dict
- Python数据类型详解(四)字典:dict
- [转载]python中将普通对象作为 字典类(dict) 使用
- 列表(list)和字典(dict)数据排序 Python
- Python 使用 dict 创建二维数据、DataFrame
- Python 实现使用dict 创建二维数据、DataFrame
- Python使用collections的Counter类对字符串、列表、字典的数据元素进行计数
- coursera-使用python访问网络数据-密歇根大学
- Python简单定义与使用字典dict的方法示例
- Python 数据类型--字典(dict)基本操作
- #小练习 使用字典保存HTMLParser解析的数据 分类: python 小练习 HTMLParser 2013-11-08 20:51 574人阅读 评论(0) 收藏
- python的【字典dict】:创建、访问、更新、删除;查看键、值、键值对;遍历;排序
- 详解Python中映射类型(字典)操作符的概念和使用
- Python学习笔记(5)--数据结构之字典dict