Inspect.getsourcelines(无.__ str__)引发令人困惑的TypeError

时间:2015-10-07 21:23:40

标签: python

今天,我的一位同事找到了有趣的案例

str(None)
'None'

所以我很自然地想看看如何定义

inspect.getsourcelines(None.__str__)
TypeError: <method-wrapper '__str__' of NoneType object at 0x91a870> is not a module, class, method, function, traceback, frame, or code object

这可能并不令人惊讶,因为在C中很可能定义了无。但这提出了一个有趣的问题,如果None.__str__不是模块,类,方法等,它是什么?

1 个答案:

答案 0 :(得分:1)

  

是什么?

这是一个非常好的问题。

>>> type(None.__str__)
<type 'method-wrapper'>
>>> None.__str__.__class__
<type 'method-wrapper'>
>>> type(None.__str__.__call__)
<type 'method-wrapper'>

现在,它取决于which version of python you use,但它肯定是一个方法包装器。