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

在这里插入图片描述

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

['__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,对齐后重新查看:

['__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

#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("成了")
#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}")

相关推荐

  1. isinstance函数详解

    2024-04-27 11:40:03       33 阅读
  2. python中isinstance()作用

    2024-04-27 11:40:03       37 阅读
  3. 记录 | python isinstance()用法

    2024-04-27 11:40:03       64 阅读
  4. 速盾:怎么通过cdn防御ddos

    2024-04-27 11:40:03       33 阅读
  5. 怎么验证公钥和私钥是一对

    2024-04-27 11:40:03       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-27 11:40:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-27 11:40:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-27 11:40:03       87 阅读
  4. Python语言-面向对象

    2024-04-27 11:40:03       96 阅读

热门阅读

  1. 【Redis 开发】分布式锁中的常见问题和Lua脚本

    2024-04-27 11:40:03       28 阅读
  2. 使用 NVM 管理 Node.js 版本

    2024-04-27 11:40:03       32 阅读
  3. leetcode 27 原地删除有序数组

    2024-04-27 11:40:03       32 阅读
  4. 嵌入式前后台(Bare-Metal RTOS-Like)架构详解

    2024-04-27 11:40:03       30 阅读
  5. Kubernetes的原理及应用详解(五)

    2024-04-27 11:40:03       31 阅读
  6. ChatGPT-税收支持新质生产力

    2024-04-27 11:40:03       28 阅读
  7. Git Tag 打标签

    2024-04-27 11:40:03       33 阅读
  8. C#算法之归并排序

    2024-04-27 11:40:03       32 阅读
  9. 消费者处理消息失败如何解决

    2024-04-27 11:40:03       40 阅读
  10. 商城数据库88张表结构(六)

    2024-04-27 11:40:03       33 阅读