Python的总体功能,如“打印” [英] Python Global Function like 'print'

查看:143
本文介绍了Python的总体功能,如“打印”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望做一个全球性的功能,在Python 3.3,如打印功能。特别是我们在我们自己的应用程序嵌入式Python,我们要公开一个简单的调试(值)的全球功能,可用于任何脚本。这是我们能够通过功能,连接到一个模块,但是为了方便它会更容易为它是全球性的,如打印(值)'做到这一点。

I am looking to make a global function in python 3.3 like the print function. In particular we have embedded python in our own application and we want to expose a simple 'debug(value)' global function, available to any script. It is possible for us to do this by attaching the function to a module, however, for convenience it would be easier for it to be global like 'print(value)'.

你怎么声明任何Python文件可用无进口,或者这是Python中的黑盒子一个全球性的功能?是否有可能从C面结合呢?

How do you declare a global function that becomes available to any python file without imports, or is this a black box in python? Is it possible to do from the C side binding?

推荐答案

这是几乎总是一个坏主意,但如果你真的想这样做...

This is almost always a bad idea, but if you really want to do it…

如果您打印出来或以其他方式检查打印功能,你会看到它的模块的 建宏 。这就是你的线索。所以,你可以这样做:

If you print out or otherwise inspect the print function, you'll see it's in the module builtins. That's your clue. So, you can do this:

debugmodule.py:

debugmodule.py:

import builtins
builtins.debug = debug

现在,一个进口debugmodule后,任何其他模块可以叫调试而不是 debugmodule.debug

Now, after an import debugmodule, any other module can just call debug instead of debugmodule.debug.

是否有可能从C面做绑定?

Is it possible to do from the C side binding?

在CPython中,C扩展模块基本上可以做同样的事情,一个纯粹的Python模块一样。或者,更简单地说,写 _debugmodule.so 在C,那么 debugmodule.py 的进口,并拷贝调试建宏

In CPython, C extension module can basically do the same thing that a pure Python module does. Or, even more simply, write a _debugmodule.so in C, then a debugmodule.py that imports it and copies debug into builtins.

如果您要嵌入CPython中,你可以通过注入功能到建宏模块在启动脚本/交互式shell前/不管,或在以后的任何这样做时间。

If you're embedding CPython, you can do this just by injecting the function into the builtins module before starting the script/interactive shell/whatever, or at any later time.

虽然这肯定工程,它不是完全清楚这是否是真正的保证的工作。如果你读的文档,它说:

While this definitely works, it's not entirely clear whether this is actually guaranteed to work. If you read the docs, it says:

作为一个实现细节,大多数模块具有 __ __建宏提供作为全局的一部分的名称。 __ __建宏的值通常是要么这个模块或本模块的 __ __字典属性的值。由于这是一个实现细节,它可能不会被Python的备用实现使用。

As an implementation detail, most modules have the name __builtins__ made available as part of their globals. The value of __builtins__ is normally either this module or the value of this module’s __dict__ attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python.

和,至少在CPython的,它实际上是 __内建__ 是被搜索的查找链,而不是建宏<部分模块或字典/ code>模块。因此,有可能是另一种实现方式可以看看东西在 __内建__ 的做法,但同时不会让建宏(或用户修改它)在 __ __建宏,在这种情况下,这是行不通的。 (由于CPython的可迄今为止唯一3.x的实现,这是很难猜测...)

And, at least in CPython, it's actually that __builtins__ module or dict that gets searched as part of the lookup chain, not the builtins module. So, it might be possible that another implementation could look things up in __builtins__ like CPython does, but at the same time not make builtins (or user modifications to it) automatically available in __builtins__, in which case this wouldn't work. (Since CPython is the only 3.x implementation available so far, it's hard to speculate…)

如果这不会对将来的Python 3.x的执行工作,我能想到的唯一的选择就是让你的功能注入到每一个模块,而不是到建宏。你可以做到这一点与一个 PEP-302 进口钩(这是一个很大在3.3比它更容易被当PEP 302是写...读为导入系统详细信息)。

If this doesn't work on some future Python 3.x implementation, the only option I can think of is to get your function injected into each module, instead of into builtins. You could do that with a PEP-302 import hook (which is a lot easier in 3.3 than it was when PEP 302 was written… read The import system for details).

而不是一个模块在2.x中,建宏自动注入的东西变成一个神奇的模块 __内建__ ,有只是魔术模块 __ __内置(注意缺少取值)。您可能会或可能不会有导入它(所以你还不如,是安全的)。你可能会或可能无法改变它。但它工作在(至少)CPython的和PyPy。

In 2.x, instead of a module builtins that automatically injects things into a magic module __builtins__, there's just a magic module __builtin__ (notice the missing s). You may or may not have to import it (so you might as well, to be safe). And you may or may not be able to change it. But it works in (at least) CPython and PyPy.

那么,什么是的右键的方式做到这一点?很简单:不是进口debugmodule ,只从debugmodule进口调试在您的所有其他模块。这样,它最终被每一个需要它的模块中的一个模块级的全球性的。

So, what's the right way to do it? Simple: instead of import debugmodule, just from debugmodule import debug in all of your other modules. That way it ends up being a module-level global in every module that needs it.

这篇关于Python的总体功能,如“打印”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆