GitHub 博客园 Nanakon

命名元组

from collections import namedtuple
Duck = namedtuple('Duck', 'bill tail')
duck = Duck('wide orange', 'long')
print(duck) #Duck(bill='wide orange', tail='long')
print(duck.bill) #wide orange

Duck2 = namedtuple('Duck2', 'bill tail')
parts = {'bill': 'wide orange', 'tail': 'long'}
print(Duck(**parts)) #Duck(bill='wide orange', tail='long')

duck3 = duck._replace(tail='magnificent', bill='crushing') #命名元组不可变 替换其中某些域的值返回一个新的命名元组
print(duck3) #Duck(bill='crushing', tail='magnificent')

 

posted on 2016-08-11 18:19  jzm17173  阅读(133)  评论(0编辑  收藏  举报

导航

轻音