【问题标题】:Inspect.getsourcelines(None.__str__) throws confusing TypeErrorInspect.getsourcelines(None.__str__) 抛出令人困惑的 TypeError
【发布时间】:2015-10-08 05:57:46
【问题描述】:

今天我的一位同事发现了一个有趣的案例

str(None)
'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

这可能并不奇怪,因为 None 最有可能在 C 中定义。但这带来了一个有趣的问题,如果None.__str__ 不是模块、类、方法等,它是什么?

【问题讨论】:

  • 那是 Python 2.7 吗?
  • 在 2.7 和 3.5 上测试

标签: python


【解决方案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,但它绝对是一个方法包装器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-03
    • 2013-07-20
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多