Python 之 Bunch Pattern
When prototyping (or even finalizing) data structures such as trees, it can be useful to have a flexible class that will allow you to specify arbitrary attributes in the constructor. In these cases, the “Bunch” pattern (named by Alex Martelli in the Python Cookbook) can come in handy. There are many ways of implementing it, but the gist of it is the following:(实现数据结构,比如说树的时候使用,有多种实现方式,要点如下)
class Bunch(dict):
def __init__(self, *args, **kwds):
super(Bunch, self).__init__(*args, **kwds)
self.__dict__ = self
There are several useful aspects to this pattern. First, it lets you create and set arbitrary ttributes by supplying them as command-line arguments:(这个pattern很有用,第一,你可一设置任意的属性)
>>> x = Bunch(name="Jayne Cobb", position="Public Relations")
>>> x.name
'Jayne Cobb'
Second, by subclassing dict, you get lots of functionality for free, such as iterating over the keys/attributes or easily checking whether an attribute is present. Here’s an example:(第二,通过子类化的dict,你可以获得很多功能,比如迭代的key-value,或者检查属性值是否存在等)
>>> T = Bunch
>>> t = T(left=T(left="a", right="b"), right=T(left="c"))
>>> t.left
{'right': 'b', 'left': 'a'}
>>> t.left.right
'b'
>>> t['left']['right']
'b'
>>> "left" in t.right
True
>>> "right" in t.right
False
This pattern isn’t useful only when building trees, of course. You could use it for any situation where you’d want a flexible object whose attributes you could set in the constructor.(不仅仅用于建树)
Python 之 Bunch Pattern的更多相关文章
- python singleton design pattern super() 多继承
python singleton design pattern decorate baseclass metaclass import module super() 一.A decorator de ...
- [Python] 机器学习库资料汇总
声明:以下内容转载自平行宇宙. Python在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包.包括: 一个强大的N维数组对象Array: ...
- 【转帖】Python在大数据分析及机器学习中的兵器谱
Flask:Python系的轻量级Web框架. 1. 网页爬虫工具集 Scrapy 推荐大牛pluskid早年的一篇文章:<Scrapy 轻松定制网络爬虫> Beautiful Soup ...
- python数据挖掘领域工具包
原文:http://qxde01.blog.163.com/blog/static/67335744201368101922991/ Python在科学计算领域,有两个重要的扩展模块:Numpy和Sc ...
- 2016年GitHub排名前20的Python机器学习开源项目(转)
当今时代,开源是创新和技术快速发展的核心.本文来自 KDnuggets 的年度盘点,介绍了 2016 年排名前 20 的 Python 机器学习开源项目,在介绍的同时也会做一些有趣的分析以及谈一谈它们 ...
- [resource]Python机器学习库
reference: http://qxde01.blog.163.com/blog/static/67335744201368101922991/ Python在科学计算领域,有两个重要的扩展模块: ...
- python 学习笔记 10 -- 正則表達式
零.引言 在<Dive into Python>(深入python)中,第七章介绍正則表達式,开篇非常好的引出了正則表達式,以下借用一下:我们都知道python中字符串也有比較简单的方法, ...
- python编程基础—正则表达式
正则表达式 正则表达式就是描述字符串排列的一套规则.利用正则表达式可以做很多事情,主要用于字符串的匹配 在实际项目中,我们经常需要找到某一类符合某种格式的信息,此时,我们可以观察这些数据的规律,然后将 ...
- [转]Python机器学习工具箱
原文在这里 Python在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包.包括: 一个强大的N维数组对象Array: 比较成熟的(广播 ...
随机推荐
- oracle中的timestamp字段的值乱码问题修改
我的解决方案: 直接新增一个系统变量: key值为:NLS_TIMESTAMP_FORMATvalue的值为:YYYY-MM-DD HH24:MI:SSFF6 其它解决方案: 在登录PLSQL之后,查 ...
- Windows资源管理器 已停止工作
解决方案:http://jingyan.baidu.com/article/5225f26b6aa830e6fa0908a8.html
- 遍历所有的选中的radio的个数和值
var ID = ""; $('input:radio:checked').each(function (i) { ...
- writel(readl(&pwm_timer->tcfg0) | 0xff, &pwm_timer->tcfg0);
解析这句代码什么意思! 神说:选定预分频器0 为什么? 神说:因为实验中选的是timer1,在预分频器0下: 若选择timer4,该如何写这句代码? 首先看tcfg0中选择预分频器1,在看tcfg1里 ...
- PHP时间日期
PHP常用的几个时间日期函数有:date(),mktime(),strtotime(); 一.string date ( string $format [, int $timestamp ] ) 函数 ...
- 利用spring、cxf编写并发布webservice
配置文件spring-wsServer.xml <?xml version="1.0" encoding="UTF-8"?> <beans x ...
- i-doit
官网:http://www.i-doit.org/,有免费版和专业版. 开源:http://sourceforge.net/projects/i-doit/ › Features CMDB I ...
- JavaScript 基础(七) 箭头函数 generator Date JSON
ES6 标准新增了一种新的函数: Arrow Function(箭头函数). x => x *x 上面的箭头相当于: function (x){ return x*x; } 箭头函数相当于匿名函 ...
- JavaWeb项目连接Oracle数据库
参考网址:http://jingyan.baidu.com/article/0320e2c1d4dd0b1b87507b38.html 既然你要链接oracle数据库 ,那么首先就是先打开我们的ora ...
- jafka消息结构