如何添加内置函数?
我是Python编程的新手。如何使用C或C ++将新的内置功能和关键字添加到Python解释器?
I am new to python programming. How can I add new built-in functions and keywords to python interpreter using C or C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简而言之,从技术上讲,有可能将东西添加到Python的内置†中,但几乎从来没有必要(通常认为是一个非常糟糕的主意)。
更长的时候,显然可以修改Python的来源并添加新的内置,关键字等……但是这样做的过程有点超出了该问题的范围。
如果您想了解如何修改Python源,如何编写可以从Python调用的C函数或其他内容的详细信息,请编辑问题以使其更具体。
如果您是Python编程的新手,并且您会觉得自己应该在日常工作中修改核心语言,那么这可能是一个指标,您应该只是简单地了解它。用于大量不同的问题域(例如, numpy 是一种促进科学的扩展,计算和 Blender 将其用于3D动画),因此该语言也可以处理您的问题域。
†:您 can 修改
__内置__
模块“添加新的内置”……但这几乎可以肯定是一个坏主意:任何依赖它的代码都会非常困难(并且令人困惑的)在其原始应用程序的上下文之外使用任何地方。例如,考虑一下,如果您添加greate_than_zero
“ hindinin”,请在其他地方使用它:任何试图读取该代码的人都会感到困惑,因为他们不知道
oreal_than_zero 是定义的,任何试图从没有偷偷摸摸
无法使用的应用程序中使用该代码的任何人。greate_than_zero>
<代码> __内置__ _____一种更好的方法是使用Python的现有
导入
语句: http:// docs.python.org/tutorial/modules.htmlIn short, it is technically possible to add things to Python's builtins†, but it is almost never necessary (and generally considered a very bad idea).
In longer, it's obviously possible to modify Python's source and add new builtins, keywords, etc… But the process for doing that is a bit out of the scope of the question as it stands.
If you'd like more detail on how to modify the Python source, how to write C functions which can be called from Python, or something else, please edit the question to make it more specific.
If you are new to Python programming and you feel like you should be modifying the core language in your day-to-day work, that's probably an indicator you should simply be learning more about it. Python is used, unmodified, for a huge number of different problem domains (for example, numpy is an extension which facilitates scientific computing and Blender uses it for 3D animation), so it's likely that the language can handle your problem domain too.
†: you can modify the
__builtin__
module to “add new builtins”… But this is almost certainly a bad idea: any code which depends on it will be very difficult (and confusing) to use anywhere outside the context of its original application. Consider, for example, if you add agreater_than_zero
“builtin”, then use it somewhere else:Anyone who tries to read that code will be confused because they won't know where
greater_than_zero
is defined, and anyone who tries to use that code from an application which hasn't snuckgreater_than_zero
into__builtin__
won't be able to use it.A better method is to use Python's existing
import
statement: http://docs.python.org/tutorial/modules.html对于 python 3.6 及以上版本,请使用
importbuiltins
。for python 3.6 onward use
import builtins
.虽然 David Wolever 的答案很完美,但需要再次注意的是,提问者是 Python 新手。基本上他想要的只是一个全局函数,可以通过两种不同的方式完成......
我认为提问者的解决方案是第二个选项,任何刚接触 Python 并有这个问题的人都应该研究同样的问题。
对于高级用户,我同意 Wolever 的建议,即在内置模块中插入新函数是一个坏主意。但是,用户可能正在寻找一种方法来避免在项目中的每个脚本中导入始终使用的模块。这是一个有效的用例。当然,这些代码对于不属于该项目的人来说没有意义,但这不应该是一个问题。无论如何,此类用户应该查看 PYTHONSTARTUP 环境变量。我建议在 Python 文档的索引中查找它,并查看所有讨论此环境变量的链接,看看哪个页面符合您的目的。但是,该解决方案仅适用于交互模式,不适用于子主脚本。
如需全方位解决方案,请查看我已实现的此功能:https://drive.google.com/file/d/19lpWd_h9ipiZgycjpZW01E34hbIWEbpa/view
另一种方法是扩展或嵌入 Python,它是比较复杂的话题。最好阅读相关的 Python 文档。对于基本用户,我想说的是……
高级用户已经知道他们在做什么!
While David Wolever's answer is perfect, it should be noted again that the asker is new to Python. Basically all he wants is a global function, which can be done in two different ways...
I think the asker's solution is the 2nd option and anyone new to Python having this question should look in to the same.
For an advance user, I would agree with Wolever's suggestion that it is a bad idea to insert a new function in to the builtin module. However, may be the user is looking for a way to avoid importing an always-used module in every script in the project. And that is a valid use case. Of course the code will not make sense to people who aren't part of the project but that shouldn't be a concern. Anyways, such users should look in to the PYTHONSTARTUP environment variable. I would suggest looking it up in the Index of the Python documentation and look at all links that talks about this environment variable and see which page serves your purpose. However, this solution works for interactive mode only and does not work for sub-main script.
For an all around solution look in to this function that I have implemented: https://drive.google.com/file/d/19lpWd_h9ipiZgycjpZW01E34hbIWEbpa/view
Yet another way is extending or embedding Python and it is a relatively complex topic. It is best to read the Python documentation on the same. For basic users, all I would say is that...
And advanced users already know what they are doing!
您可以使用
builtins
模块。示例 1:
示例 2:
You can use
builtins
module.Example 1:
Example 2:
不导入
builtins
模块:Without importing
builtins
module: