加入收藏 | 设为首页 | 会员中心 | 我要投稿 莱芜站长网 (https://www.0634zz.com/)- 云连接、建站、智能边缘云、设备管理、大数据!
当前位置: 首页 > 编程开发 > Python > 正文

python C 让namedtuple接受kwargs

发布时间:2023-12-21 18:26:07 所属栏目:Python 来源:DaWei
导读: 如果我有一个类:


class Person(object):
def __init__(self,name,**kwargs):
self.name = name

p = Person(name='joe',age=25) # age is ignored
额外的参数被忽略

如果我有一个类: 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,这样我才能安全地传递额外的参数?

解决方法 解释器中的以下会话显示了解决问题的一种可能解决方案: 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,**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',verbose,rename) return type(typename,(base,),{ '__slots__': (),'__new__': lambda cls,**kwargs: base.__new__(cls,**{ key: value for key,value in kwargs.items() if key in base._fields})}) if __name__ == '__main__': main()

(编辑:莱芜站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章