2024-10-04 09:28:36 发布
网友
例如,在伪代码中:
class Example: def __init__(self, dict): for key, value in dict.items(): self.key = value a = Example({"objectVariable": ["some", "data"]}) print(a.objectVariable) >>>["some", "data"]
我将如何实现这一点?你知道吗
提前谢谢
将dict分配给内置的__dict__以获得更大的简化:
dict
__dict__
class Example: def __init__(self, dict): self.__dict__ = dict
您正在查找__getattr__,如果插槽不存在,将调用它。你知道吗
__getattr__
class Example: def __init__(self, dict): self.dict = dict def __getattr__(self, prop): return self.dict[prop]
将
dict
分配给内置的__dict__
以获得更大的简化:您正在查找
__getattr__
,如果插槽不存在,将调用它。你知道吗相关问题 更多 >
编程相关推荐