如何获取内置类的源代码?

huangapple go评论70阅读模式
英文:

How can get the source code of built-in class?

问题

property 是 Python 中的一个类。

type(property)
<class 'type'>
callable(property)
True

我想要获取这个内置类的源代码:

import inspect
inspect.getsource(property)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/inspect.py", line 1024, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/lib/python3.9/inspect.py", line 1006, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python3.9/inspect.py", line 817, in findsource
    file = getsourcefile(object)
  File "/usr/lib/python3.9/inspect.py", line 697, in getsourcefile
    filename = getfile(object)
  File "/usr/lib/python3.9/inspect.py", line 666, in getfile
    raise TypeError('{!r} is a built-in class'.format(object))
TypeError: <class 'property'> is a built-in class
英文:

property is a class in python.

type(property)
&lt;class &#39;type&#39;&gt;
callable(property)
True

I want to get the source code of the built-in class:

import inspect
inspect.getsource(property)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/lib/python3.9/inspect.py&quot;, line 1024, in getsource
    lines, lnum = getsourcelines(object)
  File &quot;/usr/lib/python3.9/inspect.py&quot;, line 1006, in getsourcelines
    lines, lnum = findsource(object)
  File &quot;/usr/lib/python3.9/inspect.py&quot;, line 817, in findsource
    file = getsourcefile(object)
  File &quot;/usr/lib/python3.9/inspect.py&quot;, line 697, in getsourcefile
    filename = getfile(object)
  File &quot;/usr/lib/python3.9/inspect.py&quot;, line 666, in getfile
    raise TypeError(&#39;{!r} is a built-in class&#39;.format(object))
TypeError: &lt;class &#39;property&#39;&gt; is a built-in class

答案1

得分: 1

由于您要求只翻译代码部分,以下是您提供的代码片段的中文翻译:

您之所以会遇到这个错误,是因为在CPython(最流行的Python实现,也是您使用的实现)中,property 是在C中原生实现的,没有Python源代码可供显示。

由于CPython是开源的,您可以自行查找C源代码。对于 property,它在这里声明:

https://github.com/python/cpython/blob/58f5227d7cdff803609a0bda6882997b3a5ec4bf/Objects/descrobject.c#L1958-L2000

您可以查看引用的符号以查看详细信息,例如 property_methods 是该类的方法列表,定义在这里:

https://github.com/python/cpython/blob/58f5227d7cdff803609a0bda6882997b3a5ec4bf/Objects/descrobject.c#L1575-L1581

英文:

You're getting this error because in CPython (the most popular implementation of Python, and the one that's you're using) property is implemented natively in C, and has no Python source to show.

Since CPython is open-source, you can poke around and find the C source yourself. For property, it's declared here:

https://github.com/python/cpython/blob/58f5227d7cdff803609a0bda6882997b3a5ec4bf/Objects/descrobject.c#L1958-L2000

You can look at the referenced symbols to see the details, e.g. property_methods is the list of this class' methods, defined here:

https://github.com/python/cpython/blob/58f5227d7cdff803609a0bda6882997b3a5ec4bf/Objects/descrobject.c#L1575-L1581

huangapple
  • 本文由 发表于 2023年6月12日 08:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定