繁体   English   中英

Python3 - 替换动态选择的namedtuple字段

[英]Python3 — replacing a dynamically chosen namedtuple field

我正在使用collections.namedtuple数据库记录建模。 有时,我希望用户能够替换任意字段的内容。 _replace()方法允许替换特定字段的内容,只要我们可以将其名称指定为参数的一部分: somenamedtuple._replace(somefield=newcontent) 但如果名称本身是由用户动态提供的,我无法找到方法。

这是一个最小的工作示例:

from collections import namedtuple

fields = ['one', 'two', 'three']
Record = namedtuple('Record', fields)
# Populate fields.
record = Record(*tuple(['empty' for i in fields]))
while True:
    # Show what we have already.
    print('0: quit')
    for i in range(len(fields)):
    print('{}: {}: {}'.format(i+1, fields[i], record[i]))
    to_change = int(input('Field to change: '))
    if not to_change:
        break
    else:
        new_content = input('New content: ')
        field_to_change = {fields[to_change-1]:new_content}
        print('Setting', field_to_change)
        record._replace(**field_to_change)
print('Finished.')
print(record)

输出(Ipython 1.0.0,Python 3.3.1)如下。

In [1]: run test_setattr_namedtuple
0: quit
1: one: empty
2: two: empty
3: three: empty
Field to set: 2
New content: asdf
Setting {'two': 'asdf'}
0: quit
1: one: empty
2: two: empty
3: three: empty
Field to set: 0
Finished.
Record(one='empty', two='empty', three='empty')

In [2]: 

record._replace()行试图将'two'设置为'asdf',而不是two ,因此无声地失败。 我曾想过在_replace()中使用eval ,但_replace()不接受表达式作为参数。

我也尝试了内置函数setattr ,但它不适用于namedtuples,大概是因为它们是不可变的。

._replace()方法返回已更改的命名元组实例。 您正在丢弃返回的值。

像元组,namedtuple派生的类的实例是不可变的,并且._replace() 改变就地值。

用新的替换原始值:

record = record._replace(**field_to_change)                                         

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM