python - Why inspect.getsource only works for functions and not classes in jupyter? - Stack Overflow

import inspectdef method():return "Hello"source_code = inspect.getsource(method)print(source

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744936010a4602028.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信