前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >怎么通过isinstance(Obj,Class)验证?【isinstance】

怎么通过isinstance(Obj,Class)验证?【isinstance】

作者头像
来杯Sherry
发布2024-04-27 09:06:02
980
发布2024-04-27 09:06:02
举报
文章被收录于专栏:第一专栏第一专栏

最近有这样一个项目,这个项目可以用一个成熟的项目的构造树,读取树,再检索的过程,现在有新的需求,另一个逻辑构造同样节点结构的树,pickle序列化保存,再使用原来项目的读取、检索函数,当我完成存储正准备读取的时候,这个成熟的项目的健壮性,拒绝了我的树,这里的拒绝逻辑具体用到了instance,这是一个判断obj是不是class的实例的python内置函数,(据说,obj是继承的子类也过,感兴趣可以验证下,我这里的任务是识别为该类实例化的对象) 使用dir(my_tree)dir(original_tree)可以看到对象属性方法的不同:

代码语言:javascript
复制
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'all_nodes', 'build_hierarchy', 'build_tree', 'create_nodes', 'layer_to_nodes', 'leaf_nodes', 'markdown_text', 'num_layers', 'parse_markdown', 'root_nodes', 'save']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'all_nodes', 'layer_to_nodes', 'leaf_nodes', 'num_layers', 'root_nodes']

我尽可能能让我构造的类MyTree对齐原本的类Tree,对齐后重新查看:

代码语言:javascript
复制
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'all_nodes', 'layer_to_nodes', 'leaf_nodes', 'num_layers', 'root_nodes']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'all_nodes', 'layer_to_nodes', 'leaf_nodes', 'num_layers', 'root_nodes']

这里有很高概率可以通过isinstance(my_tree,Tree) == True,如果你和我一样仍然不通过,试试对齐模块路径,我这里成了。

不同的模块路径:如果 Tree 类是在不同的模块中定义的,并且 pickle 在序列化时保存了模块路径信息,那么即使类名相同,如果模块路径不同,isinstance() 也可能会返回 False

代码语言:javascript
复制
#pickle反序列化过程参考
SAVE_PATH = "output/chapter4_tree"
    import pickle
    from raptor.tree_structures import Tree
    with open(SAVE_PATH, "rb") as file:
                    tree = pickle.load(file)
    print(dir(tree))
    
    original_tree = Tree(all_nodes={}, root_nodes=[], leaf_nodes=[], num_layers=0, layer_to_nodes={})
    print(dir(original_tree))
    if not isinstance(tree,Tree):
        print("还得改") 
    else:
        print("成了")
代码语言:javascript
复制
#pickle序列化过程参考
def save(self,tree, path):
    # 确保路径中的目录存在
    self.tree = tree
    os.makedirs(os.path.dirname(path), exist_ok=True)
    
    with open(path, 'wb') as file:
        pickle.dump(self.tree, file)
    print(f"Tree successfully saved to {path}")
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com