相关疑难解决方法(0)

为什么检查会从继承超类的类返回不同的行?

在试图弄清楚是否使用@decorator语法调用函数时,我们意识到inspect在查看从超类继承的装饰类时会有不同的行为.

在Windows 10下,CPython 3.6.2发现了以下行为.

它也在Linux 64位下在CPython 3.7.0中重现.

import inspect

def decorate(f):
    lines = inspect.stack()[1].code_context
    print(f.__name__, lines)

    return f

@decorate
class Foo:
    pass

@decorate
class Bar(dict):
    pass
Run Code Online (Sandbox Code Playgroud)

产量

Foo ['@decorate\n']
Bar ['class Bar(dict):\n']
Run Code Online (Sandbox Code Playgroud)

为什么继承改变了行为inspect

python decorator inspect python-decorators python-3.6

6
推荐指数
1
解决办法
71
查看次数