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的更多相关文章

  1. python singleton design pattern super() 多继承

    python  singleton design pattern decorate baseclass metaclass import module super() 一.A decorator de ...

  2. [Python] 机器学习库资料汇总

    声明:以下内容转载自平行宇宙. Python在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包.包括: 一个强大的N维数组对象Array: ...

  3. 【转帖】Python在大数据分析及机器学习中的兵器谱

    Flask:Python系的轻量级Web框架. 1. 网页爬虫工具集 Scrapy 推荐大牛pluskid早年的一篇文章:<Scrapy 轻松定制网络爬虫> Beautiful Soup ...

  4. python数据挖掘领域工具包

    原文:http://qxde01.blog.163.com/blog/static/67335744201368101922991/ Python在科学计算领域,有两个重要的扩展模块:Numpy和Sc ...

  5. 2016年GitHub排名前20的Python机器学习开源项目(转)

    当今时代,开源是创新和技术快速发展的核心.本文来自 KDnuggets 的年度盘点,介绍了 2016 年排名前 20 的 Python 机器学习开源项目,在介绍的同时也会做一些有趣的分析以及谈一谈它们 ...

  6. [resource]Python机器学习库

    reference: http://qxde01.blog.163.com/blog/static/67335744201368101922991/ Python在科学计算领域,有两个重要的扩展模块: ...

  7. python 学习笔记 10 -- 正則表達式

    零.引言 在<Dive into Python>(深入python)中,第七章介绍正則表達式,开篇非常好的引出了正則表達式,以下借用一下:我们都知道python中字符串也有比較简单的方法, ...

  8. python编程基础—正则表达式

    正则表达式 正则表达式就是描述字符串排列的一套规则.利用正则表达式可以做很多事情,主要用于字符串的匹配 在实际项目中,我们经常需要找到某一类符合某种格式的信息,此时,我们可以观察这些数据的规律,然后将 ...

  9. [转]Python机器学习工具箱

    原文在这里  Python在科学计算领域,有两个重要的扩展模块:Numpy和Scipy.其中Numpy是一个用python实现的科学计算包.包括: 一个强大的N维数组对象Array: 比较成熟的(广播 ...

随机推荐

  1. string.Format 格式化输出日期

    string.Format("{0:d}",System.DateTime.Now) 结果为:2009-3-20 (月份位置不是03) string.Format("{0 ...

  2. java 调用 r, Can't find dependent libraries

    rJava是一个R语言和Java语言的通信接口,通过底层JNI实现调用,允许在R中直接调用Java的对象和方法. 步骤: 1.本地系统: Win7 64bit 企业版, jdk1.8.0_45,R3. ...

  3. 解决ewebeditor for php在IE8下报editor.js错误的解决方案

    打开editor.js,寻找如下代码: if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()"); ...

  4. 树莓派USB摄像头与camera模块对比

    http://www.cnblogs.com/weixinforspurs/p/5575962.html ——————————————————————————————————————————————— ...

  5. HashSet中的元素必须重写equals方法和hashCode方法

    http://jingyan.baidu.com/article/d5a880eb8fb61d13f147cc99.html 1.为什么必须重写这两个方法. 2.什么事hashSet去重,符合什么样的 ...

  6. c++学习笔记——构造函数

    构造函数定义:每个类都分别定义了它的对象被初始化的方式,类通过一个或几个特殊的成员函数来控制其对象的初始化过程,这些函数叫做构造函数. 需要注意的几点: 1:构造函数不能被声明为const的,当我们创 ...

  7. 关于c#中的console用法大全

    C#之Console   Console.Write  表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入.Console.WriteLine  表示向控制台写入字符串后换行.Conso ...

  8. 笔记本Linux推荐

    1.CUB LINUX Cub Linux 是一个最好的选择.他的前身来自著名的 Chromium OS , Cub Linux 能够运行在各种各样的笔记本上面.即便是早年的老机,亦或是现在的新机.从 ...

  9. C#控件根据窗体改变大小

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  10. 开源PLM软件Aras详解四 ItemType的概念

    首先,我们需要了解什么是ItemType,俗称对象类 官方一点就是ItemType是一个用来定义业务对象类的业务对象类 通过ItemType定义的每个业务对象类可以产生各自的实例对象 通过ItemTy ...