簡體   English   中英

如何獲取python打印功能的源代碼?

[英]how to get source code of python print function?

我正在嘗試使用inspect獲取python打印的源代碼,但它拋出錯誤。 有人可以告訴我如何獲取python2.7打印語句的源代碼。

inspect.getsourcefile(print)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-85-6158f5314751> in <module>()
----> 1 inspect.getsourcefile(print)

/usr/lib/python3.4/inspect.py in getsourcefile(object)
    569     Return None if no way can be identified to get the source.
    570     """
--> 571     filename = getfile(object)
    572     all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
    573     all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]

/usr/lib/python3.4/inspect.py in getfile(object)
    534         return object.co_filename
    535     raise TypeError('{!r} is not a module, class, method, '
--> 536                     'function, traceback, frame, or code object'.format(object))
    537 
    538 ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')

TypeError: <built-in function print> is not a module, class, method, function, traceback, frame, or code object

In [86]: inspect.getsourcefile(builtin.print)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-86-2d1fb60aac58> in <module>()
----> 1 inspect.getsourcefile(builtin.print)

NameError: name 'builtin' is not defined

print功能以C語言實現。 這就是為什么您無法通過inspect模塊獲取其源代碼的原因。 代碼在這里: https : //github.com/python/cpython/blob/2.7/Python/bltinmodule.c#L1580

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM