admin管理员组

文章数量:1410682

import inspect


def method():
    return "Hello"

source_code = inspect.getsource(method)
print(source_code)

works in both jupyter notebook and .py file

import inspect


class MyClass:
    def method(self):
        return "Hello"


source_code = inspect.getsource(MyClass)
print(source_code)

works in .py file but in jupyter it's OSError: source code not available.

What's going on?

Can I understand it is py scripts can inspect all code? What else can or cannot be inspected in jupyter?

Why does this answer () say it errors in interactive shell but both my function and that function works for me in both jupyter and ipython? (my python is 3.10.15, that one is 3.10.6)

本文标签: pythonWhy inspectgetsource only works for functions and not classes in jupyterStack Overflow