英文:
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)
<class 'type'>
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 "<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
答案1
得分: 1
由于您要求只翻译代码部分,以下是您提供的代码片段的中文翻译:
您之所以会遇到这个错误,是因为在CPython(最流行的Python实现,也是您使用的实现)中,property
是在C中原生实现的,没有Python源代码可供显示。
由于CPython是开源的,您可以自行查找C源代码。对于 property
,它在这里声明:
您可以查看引用的符号以查看详细信息,例如 property_methods
是该类的方法列表,定义在这里:
英文:
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:
You can look at the referenced symbols to see the details, e.g. property_methods
is the list of this class' methods, defined here:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论