如果我有这样的类(class):
class Person(object):
def __init__(self, name, **kwargs):
self.name = name
p = Person(name='joe', age=25) # age is ignored
额外的参数被忽略。但是如果我有一个
namedtuple
,我会得到`意外的关键字参数:from collections import namedtuple
Person = namedtuple('Person', 'name')
p = Person(name='joe', age=25)
# Traceback (most recent call last):
# File "python", line 1, in <module>
# TypeError: __new__() got an unexpected keyword argument 'age'
如何让
namedtuple
接受 kwargs
以便我可以安全地传递额外的参数? 最佳答案
解释器中的以下 session 显示了解决问题的一种可能解决方案:
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import collections
>>> class Person(collections.namedtuple('base', 'name')):
__slots__ = ()
def __new__(cls, *args, **kwargs):
for key in tuple(kwargs):
if key not in cls._fields:
del kwargs[key]
return super().__new__(cls, *args, **kwargs)
>>> p = Person(name='joe', age=25)
>>> p
Person(name='joe')
>>>
替代:
由于您宁愿拥有更简单的解决方案,您可能会发现下一个程序更符合您的喜好:
#! /usr/bin/env python3
import collections
def main():
Person = namedtuple('Person', 'name')
p = Person(name='joe', age=25)
print(p)
def namedtuple(typename, field_names, verbose=False, rename=False):
base = collections.namedtuple('Base', field_names, verbose, rename)
return type(typename, (base,), {
'__slots__': (),
'__new__': lambda cls, *args, **kwargs: base.__new__(cls, *args, **{
key: value for key, value in kwargs.items()
if key in base._fields})})
if __name__ == '__main__':
main()
关于python - 使 namedtuple 接受 kwargs,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35727396/