python使用点操作符访问字典(dict)数据的方法,pythondict
python使用点操作符访问字典(dict)数据的方法,pythondict
本文实例讲述了python使用点操作符访问字典(dict)数据的方法。分享给大家供大家参考。具体分析如下:
平时访问字典使用类似于:dict['name']的方式,如果能通过dict.name的方式访问会更方便,下面的代码自定义了一个类提供了这种方法。
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.bkjia.com print(d.foo) # AttributeError: 'DottableDict' object has no attribute 'foo'
希望本文所述对大家的Python程序设计有所帮助。
相关内容
- 使用Python实现下载网易云音乐的高清MV,python高清
- Python实现windows下模拟按键和鼠标点击的方法,python鼠标
- Python实现遍历windows所有窗口并输出窗口标题的方法,
- python实现根据窗口标题调用窗口的方法,python窗口标题
- python根据时间生成mongodb的ObjectId的方法,mongodbobjectid
- python使用chardet判断字符串编码的方法,pythonchardet
- python通过正则查找微博@(at)用户的方法,pythonat
- python将MongoDB里的ObjectId转换为时间戳的方法,mongodbob
- python通过urllib2获取带有中文参数url内容的方法,
- Python比较两个图片相似度的方法,
评论关闭