• 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连接MySQL并使用fetchall()方法过滤特殊字符
  • Python Queue模块详细介绍及实例
  • 简单学习Python time模块
  • 用Python实现随机森林算法的示例
  • python完成FizzBuzzWhizz问题(拉勾网面试题)示例
  • Python的GUI框架PySide的安装配置教程
  • Python字符串替换实例分析
  • 在Python中处理字符串之ljust()方法的使用简介
  • python检测某个变量是否有定义的方法

文章分类

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

最近更新的内容

    • Python HTTP客户端自定义Cookie实现实例
    • 在Python中使用NLTK库实现对词干的提取的教程
    • python中将字典转换成其json字符串
    • 使用Python判断质数(素数)的简单方法讲解
    • Python and、or以及and-or语法总结
    • Python实现监控程序执行时间并将其写入日志的方法
    • 使用PyV8在Python爬虫中执行js代码
    • Python实现计算最小编辑距离
    • python实现应用程序在右键菜单中添加打开方式功能
    • Python迭代和迭代器详解

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

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