• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python使用点操作符访问字典(dict)数据的方法

python使用点操作符访问字典(dict)数据的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python dict,python dict函数,python dict遍历,python dict排序,python中dict等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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.jb51.net
print(d.foo)
# AttributeError: 'DottableDict' object has no attribute 'foo'
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python实现字典(dict)和字符串(string)的相互转换方法
  • Python用list或dict字段模式读取文件的方法
  • Python用list或dict字段模式读取文件的方法
  • Python 提取dict转换为xml/json/table并输出的实现代码
  • 如何将python中的List转化成dictionary
  • python dict.get()和dict['key']的区别详解
  • python 字典(dict)按键和值排序
  • Python数据类型详解(四)字典:dict
  • python 的列表遍历删除实现代码
  • 详解Python中dict与set的使用

相关文章

  • 常用python编程模板汇总
  • Python使用pygame模块编写俄罗斯方块游戏的代码实例
  • Python中getattr函数和hasattr函数作用详解
  • Python中Django发送带图片和附件的邮件
  • python 基础学习第二弹 类属性和实例属性
  • python抓取最新博客内容并生成Rss
  • 安装ElasticSearch搜索工具并配置Python驱动的方法
  • Python中多线程thread与threading的实现方法
  • python中OrderedDict的使用方法详解
  • tensorflow实现测试时读取任意指定的check point的网络参数

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 浅谈Python中copy()方法的使用
    • Win7下搭建python开发环境图文教程(安装Python、pip、解释器)
    • python计算时间差的方法
    • 在Python中的Django框架中进行字符串翻译
    • 在漏洞利用Python代码真的很爽
    • python pickle 和 shelve模块的用法
    • 详解Python中列表和元祖的使用方法
    • 分享一个常用的Python模拟登陆类
    • Python程序语言快速上手教程
    • python网络编程示例(客户端与服务端)

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有