淘宝网特卖,车辆违章记录查询官网,安徽省人才市场
本文实例讲述了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 //www.jb51.net print(d.foo) # AttributeError: 'DottableDict' object has no attribute 'foo'
希望本文所述对大家的Python程序设计有所帮助。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论