Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 填充嵌套对象什么仅支持kwargs进行初始化?_Python_Keyword Argument_Apache Pulsar - Fatal编程技术网

Python 填充嵌套对象什么仅支持kwargs进行初始化?

Python 填充嵌套对象什么仅支持kwargs进行初始化?,python,keyword-argument,apache-pulsar,Python,Keyword Argument,Apache Pulsar,我有一些带有记录类的python库。 记录类仅使用kwargs接受数据。 我像这样填充记录,它工作正常: class Animal(Record): name = String(required=True) animal = Animal(**{'name': 'joe'}) 该库还支持如下嵌套记录: class Fur(Record): color = String(required=True) class Animal(Record): fur = Fur

我有一些带有记录类的python库。 记录类仅使用
kwargs
接受数据。 我像这样填充记录,它工作正常:


class Animal(Record):
    name = String(required=True)


animal = Animal(**{'name': 'joe'})
该库还支持如下嵌套记录:

class Fur(Record):
    color = String(required=True)


class Animal(Record):
    fur = Fur(required=True)

但是,当我尝试填充以下内容时:

animal = Animal(**{'fur': {'color': 'red'}})
它失败,因为子记录不接收
color=red
,而是接收
{'color':'red'}


所以我需要一种“递归**”

简单地做一下怎么样:

animal = Animal(fur = Fur(color = 'red'))
关于kwargs的更多信息:

不过,我不认为你能用这样一本未经加工的字典做到这一点。如果查看Record类的
\uuuu init\uuu
(),您将看到kwargs只覆盖字段的默认值,并使用
setattr
设置。因此,如果执行
animal=animal(**{'fur':{'color':'red'}})
,只需将字典
{'color':'red'}
影响到字段fur

我能想到的唯一方法是重写Record类,例如类似这样的内容(未测试):


记录的定义是什么?它在这里:对于记录,你知道你可以这样写,对吗
animal=animal(name='joe')
yes但目标是直接用嵌套的JSON填充记录目标是直接用嵌套的JSON填充记录
class SRecord(Record):
    def __setattr__(self, key, value):
        if type(self._fields.get(key, None)) is SRecord:
            value = SRecord(value)
        super(SRecord, self).__setattr__(key, value)