class Struct: def __init__(self, **entries): self.__dict__.update(entries) args = {'a': 1, 'b': 2} s = Struct(**args) >>> s.a 1 >>> s.b 2
from collections import namedtuple args = {'a': 1, 'b': 2} object_name = namedtuple("ObjectName", args.keys())(*args.values()) object_name.Name
“ObjectName” 是对象的名字,这个可以随便起。