怎么通过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

python 复制代码
#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("成了")
python 复制代码
#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}")
相关推荐
朴拙数科22 分钟前
Stable Diffusion秋叶整合包V4独立版Python本地API连接指南
开发语言·python·stable diffusion
明明跟你说过1 小时前
LangChain + 文档处理:构建智能文档问答系统 RAG 的实战指南
人工智能·python·语言模型·自然语言处理·chatgpt·langchain·gpt-3
架构师那点事儿1 小时前
人工智能基础-NumPy-向量矩阵数组库
人工智能·python·numpy
程序员晚枫2 小时前
从零打造你的专属Python第三方库
python·开源
这里有鱼汤2 小时前
__init__.py 是个啥,为什么深受大厂程序员偏爱?
后端·python
啊阿狸不会拉杆2 小时前
数据结构-数组与广义表
java·c语言·数据结构·c++·python·算法
databook3 小时前
软间隔:让支持向量机更“宽容”
python·机器学习·scikit-learn
人工智能那些事儿3 小时前
神经网络:从基础到应用,开启智能时代的大门
java·人工智能·python·深度学习·神经网络·算法·机器学习
柠石榴3 小时前
【python编程从入门到到实践】第四章 操作列表
开发语言·python
guts3503 小时前
【pytorch】torch.nn.Unfold操作
人工智能·pytorch·python