I have the following function:
def to_url(self):
return {
'ass_cls': self.model.__class__.__name__,
**{local.name: getattr(self.model.src, remote.name)
for local, remote in self.model.__class__.src.property.local_remote_pairs},
**{k: v
for k, v in self.model.__dict__.items()
if not k.startswith('_') and k != 'src'},
}
我该如何将这段代码转换为Python 3.4兼容的代码?
我相信,这段代码目前正在使用PEP 448 - 额外的解包泛化,这是Python 3.5的新特性。
dict(ass_cls=1, **dict(dict_comp_1, **dict_comp_2))
。基本上,你需要的是所有字典中获得的键组合在一起的扁平化字典。 - Ashwini Chaudhary