如何在 Python 中定义全局函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27930038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define global function in Python?
提问by flamenco
Is there a way to define a function to be global from within a class( or from within another function, as matter of fact)? Something similar to defining a global variable.
有没有办法从一个类中(或从另一个函数中,事实上)将一个函数定义为全局的?类似于定义全局变量的东西。
采纳答案by Martijn Pieters
Functions are added to the current namespace like any other name would be added. That means you can use the global
keyword inside a function or method:
函数被添加到当前命名空间,就像添加任何其他名称一样。这意味着您可以global
在函数或方法中使用关键字:
def create_global_function():
global foo
def foo(): return 'bar'
The same applies to a class body or method:
这同样适用于类主体或方法:
class ClassWithGlobalFunction:
global spam
def spam(): return 'eggs'
def method(self):
global monty
def monty(): return 'python'
with the difference that spam
will be defined immediately as top-level class bodies are executed on import.
不同之处在于spam
将立即定义为顶级类主体在导入时执行。
Like all uses of global
you probably want to rethink the problem and find another way to solve it. You could returnthe function so created instead, for example.
像所有使用一样,global
您可能想要重新思考问题并找到另一种解决方法。例如,您可以返回如此创建的函数。
Demo:
演示:
>>> def create_global_function():
... global foo
... def foo(): return 'bar'
...
>>> foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'foo' is not defined
>>> create_global_function()
>>> foo
<function foo at 0x102a0c7d0>
>>> foo()
'bar'
>>> class ClassWithGlobalFunction:
... global spam
... def spam(): return 'eggs'
... def method(self):
... global monty
... def monty(): return 'python'
...
>>> spam
<function spam at 0x102a0cb18>
>>> spam()
'eggs'
>>> monty
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'monty' is not defined
>>> ClassWithGlobalFunction().method()
>>> monty()
'python'
回答by Jasmeet Singh
You can use global to declare a global function from within a class. The problem with doing that is you can not use it with a class scope so might as well declare it outside the class.
您可以使用 global 从类中声明全局函数。这样做的问题是你不能在类范围内使用它,所以不妨在类之外声明它。
class X:
global d
def d():
print 'I might be defined in a class, but I\'m global'
>> X.d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'X' object has no attribute 'd'
>> d()
I might be defined in a class, but I'm global
回答by John Lunzer
I found a case where global does not have the desired effect: inside .pdbrc file. If you define functions in .pdbrc they will only be available from the stack frame from which pdb.set_trace() was called.
我发现 global 没有达到预期效果的情况:在 .pdbrc 文件中。如果您在 .pdbrc 中定义函数,它们将只能从调用 pdb.set_trace() 的堆栈帧中使用。
However, you can add a function globally, so that it will be available in all stack frames, by using an alternative syntax:
但是,您可以全局添加一个函数,以便它可以在所有堆栈帧中使用,方法是使用替代语法:
def cow(): print("I'm a cow")
globals()['cow']=cow
I've tested that it also works in place of the global keyword in at least the simplest case:
我已经测试过它至少在最简单的情况下也可以代替 global 关键字:
def fish():
def cow():
print("I'm a cow")
globals()['cow']=cow
Despite being more verbose, I thought it was worth sharing this alternative syntax. I have not tested it extensively so I can't comment on its limitations vs using the global keyword.
尽管更冗长,但我认为值得分享这种替代语法。我没有对它进行广泛的测试,所以我无法评论它与使用 global 关键字的局限性。