Skip to main content
 首页 » 编程设计

python之**kwargs 构造在 python 中的实现

2025年05月04日12oomusou

<分区>

我在 python 中实现了一个当前继承自 dict 的类,但实际上,我不希望它这样做。继承的主要原因是我可以使用 **kwargs 构造将内容复制到参数列表中。

我假设 python 对字典进行了某种迭代,但我找不到任何文档。

这是否可能,如果可能,如何实现?

代码示例只是为了让事情更清楚:

   class MyThing(): 
       def __init__(self): 
            self.dictionary = {} 
 
 
   thing = MyThing() 
   # code that causes thing.dictionary to be populated 
   somefunc(**thing) 

结果:

TypeError: somefunc() argument after ** must be a mapping, not instance