python - obj 必须是 type 的实例或子类型

标签 python python-3.x jupyter-notebook ipython

我尝试将 jupyter 笔记本动态加载到模块中,并在 https://github.com/axil/import-ipynb/blob/master/import_ipynb.py 中找到了一个工作代码示例。但是,由于此实现使用了一些已弃用的函数,并且我想将一些常见功能合并到一个包中,所以我想实现我自己的版本。然而,当我尝试在执行之前将 jupyter magic 代码转换为 python 代码时,我收到了这个奇怪的错误。

(Pdb) self
<IPython.core.inputsplitter.IPythonInputSplitter object at 0x102198c50>
(Pdb) IPythonInputSplitter
<class 'IPython.core.inputsplitter.IPythonInputSplitter'>
(Pdb) type(self)
<class 'IPython.core.inputsplitter.IPythonInputSplitter'>
(Pdb) IPythonInputSplitter is type(self)
False
(Pdb) super(IPythonInputSplitter, self)
*** TypeError: super(type, obj): obj must be an instance or subtype of type

以下是违规代码的一些上下文:

for cell in notebook.cells:
    if cell.cell_type == "code":
        code = self.shell.input_transformer_manager.transform_cell(cell.source)

从错误类型来看,我认为这不是ipython特有的问题,但我不太明白

最佳答案

此问题与提供的代码无关

出现此问题的原因是在实例化和类型检查之间使用 importlib.reload 重新加载了包含 IPython.core.inputsplitter.IPythonInputSplitter 的模块。

关于python - obj 必须是 type 的实例或子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56476381/

相关文章:

python - 如何让 python 3.x 在网络浏览器中输入文本

python - 有没有更好的方法来打印这些键值对?

python - 测量 Jupyter Notebook 代码单元的运行时间

python - 将 wav 文件传递​​到 IPython.display 时出错

python - 如何在 Python 中使用 Google Shortener API

python - 无法从 'Sequence' 导入名称 'keras.utils'

python - 多索引 Pandas 数据框上的 cumsum

Python 从包中安装子包

javascript - 访问 Jupyter 扩展中的数据对象

python - 如何在 mypy 中正确指定固定长度的序列?