求大神看下这个python 类的使用模式有什么问题,大神python,class Bunch(
求大神看下这个python 类的使用模式有什么问题,大神python,class Bunch(
class Bunch(dict): def __init__(self, *args, **kwds): super(Bunch, self).__init__(*args, **kwds) self.__dict__ = self
求各位大神解释一下这个模式!谢谢了
Bunch Pattern 字面意思大概是指链式的束式结构.主要用于松散的数据存储数据。具体看例子
class Bunch(dict): def __init__(self, *args, **kwargs): super(Bunch, self).__init__(*args, **kwargs) self.__dict__ = selfprint struct print struct.typeprint struct.sizeprint struct.genus.level# 输出{'type': 'flat', 'genus': {'intensity': 'hot', 'level': 'medium'}, 'BOOL': True, 'family': 'chordata', 'size': 'huge'}flathugemedium
如果是单纯的字典呢?
class Bunch(dict): def __init__(self, *args, **kwargs): super(Bunch, self).__init__(*args, **kwargs) # self.__dict__ = self pass# 输出{'type': 'flat', 'genus': {'intensity': 'hot', 'level': 'medium'}, 'BOOL': True, 'family': 'chordata', 'size': 'huge'}AttributeError: 'Bunch' object has no attribute 'type'
这里的用法,主要是能够扩展成为一个链式的结构持续的保存数据,尤其是 struct是一个Bunch的实例,而 struct.genus 又是一个 Bunch 实例。
用这个可是简洁的写出字典模拟的二叉树。大概如此吧。
编橙之家文章,
相关内容
- Python如何将set对象存入redis后并将其转化为json格式,
- 请问Python2.7 Ubuntu14.10 pip1.5.6提示readline()是什么错误,
- python数据库操作数据类型转换问题,python数据类型,数据
- 求大神帮看下python代码中type作用是什么,pythontype,imp
- Python try except源码练习中e代表什么?,pythonexcept,try:
- python Requests层参数和会话层参数是什么?,pythonrequests
- pyinstaller打包exe在别非windows平台上的的电脑运行不了,
- python有相应模块可以持续监测网络连通性吗,python连通
- Python处理书摘文件代码问题求大神帮忙,python书摘,从网
- Python怎样清空所有相关变量及函数或是list、object等信息
评论关闭