python 命名字典_python命名元组到字典

在Ubuntu 14.04lts版本的python2.7和python3.4上,__dict__属性按预期工作。_asdict方法也可以工作,但是我倾向于使用标准定义的、统一的属性api,而不是本地化的非统一api。

$python2.7美元# Works on:

# Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2

# Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux

import collections

Color = collections.namedtuple('Color', ['r', 'g', 'b'])

red = Color(r=256, g=0, b=0)

# Access the namedtuple as a dict

print(red.__dict__['r']) # 256

# Drop the namedtuple only keeping the dict

red = red.__dict__

print(red['r']) #256

看到asdict是获得表示soemthing的字典的语义方法(至少据我所知)。

如果能积累一个包含主要python版本和平台及其对__dict__的支持的表,那就太好了,目前我只有一个平台版本和两个python版本,如上面所述。| Platform | PyVer | __dict__ | _asdict |

| -------------------------- | --------- | -------- | ------- |

| Ubuntu 14.04 LTS | Python2.7 | yes | yes |

| Ubuntu 14.04 LTS | Python3.4 | yes | yes |

| CentOS Linux release 7.4.1708 | Python2.7 | no | yes |

| CentOS Linux release 7.4.1708 | Python3.4 | no | yes |

| CentOS Linux release 7.4.1708 | Python3.6 | no | yes |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值