怎么通过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}")
相关推荐
不写八个8 分钟前
Python办公自动化教程(005):Word添加段落
开发语言·python·word
_.Switch26 分钟前
Python机器学习框架介绍和入门案例:Scikit-learn、TensorFlow与Keras、PyTorch
python·机器学习·架构·tensorflow·keras·scikit-learn
赵荏苒38 分钟前
Python小白之Pandas1
开发语言·python
一眼万里*e1 小时前
fish-speech语音大模型本地部署
python·flask·大模型
结衣结衣.2 小时前
python中的函数介绍
java·c语言·开发语言·前端·笔记·python·学习
茫茫人海一粒沙2 小时前
Python 代码编写规范
开发语言·python
林浩2332 小时前
Python——异常处理机制
python
数据分析螺丝钉2 小时前
力扣第240题“搜索二维矩阵 II”
经验分享·python·算法·leetcode·面试
小蜗笔记3 小时前
在Python中实现多目标优化问题(7)模拟退火算法的调用
开发语言·python·模拟退火算法
TANGLONG2223 小时前
【C语言】数据在内存中的存储(万字解析)
java·c语言·c++·python·考研·面试·蓝桥杯