object.__new__创建对象

# python一切皆对象

class
Foo(object): def __init__(self, name, version=None): self.name = name self.version = version def __setstate__(self, state): self.name = state.get('name', 'LiShiChao') self.version = state.get('version', '0.0.1') def main(): obj = object.__new__(Foo) # 创建一个空对象 print(dir(Foo)) # 给init赋值 if hasattr(obj, '__setstate__'): obj.__setstate__({}) print(obj.version) if __name__ == '__main__': main()

 

posted @ 2022-11-15 10:48  LiShiChao  阅读(52)  评论(0编辑  收藏  举报