使用 python 中的函数声明全局变量
Declaring global variables with a function in python
这段JavaScript代码运行良好。我的问题不是修复代码本身,而是如何在 Python.
中模仿它
function setupSomeGlobals() {
// Local variable that ends up within closure
var num = 666;
// Store some references to functions as global variables
gAlertNumber = function() { alert(num); }
gIncreaseNumber = function() { num++; }
gSetNumber = function(x) { num = x; }
}
当调用 setupSomeGlobals()
时,它会声明要全局使用的新函数。这可以在 python 中以某种方式模仿吗?我不知道怎么办。 Python 函数似乎 运行 不像 JavaScript 函数,因为任何全局的东西都需要以某种方式返回。
- 您必须创建一个包含所需功能的单独文件
全球使用。
- 将此文件导入任何其他 python 文件,您应该可以开始了。
您是否有特定原因想要模仿确切的功能?如果不是这样就足够了。
我知道这是最糟糕的实现;),但我试图考虑其他可能性,它更接近问题中的 javascript 代码。
文件 1:setup_some_globals.py
num = 666
def g_alert_number():
global num
print num
def g_increase_number():
global num
num += 1
def g_set_number(x):
global num
num = x
变量 num 具有定义它的模块的范围。
文件 2:use_some.py
def use_global_functions():
from setup_some_globals import g_alert_number, g_increase_number, g_set_number
global g_alert_number
g_alert_number = g_alert_number
global g_increase_number
g_increase_number = g_increase_number
global g_set_number
g_set_number = g_set_number
use_global_functions()
g_alert_number()
g_increase_number()
g_alert_number()
g_set_number(99)
g_alert_number()
您无法访问这些功能,除非您调用 'use_global_functions()'
使用标准免责声明 请勿在实际代码中执行此操作,Python (3) 对您的 Javascript 的翻译如下:
def setup_some_globals():
# Local variable
num = 666
# You have to explicitly declare variables to be global,
# otherwise they are local.
global alert_number, increase_number, set_number
def alert_number():
# You can read a variable from an enclosing scope
# without doing anything special
print(num)
def increase_number():
# But if you want to assign to it, you need to be explicit about
# it. `nonlocal` means "in an enclosing scope, but not
# global".
nonlocal num
num += 1
def set_number(x):
# Same as above
nonlocal num
num = x
# Usage:
>>> setup_some_globals()
>>> set_number(3)
>>> increase_number()
>>> alert_number()
4
但如果您真的这样做了,那么几乎可以肯定有更好的方法来做您想做的事。
这段JavaScript代码运行良好。我的问题不是修复代码本身,而是如何在 Python.
中模仿它function setupSomeGlobals() {
// Local variable that ends up within closure
var num = 666;
// Store some references to functions as global variables
gAlertNumber = function() { alert(num); }
gIncreaseNumber = function() { num++; }
gSetNumber = function(x) { num = x; }
}
当调用 setupSomeGlobals()
时,它会声明要全局使用的新函数。这可以在 python 中以某种方式模仿吗?我不知道怎么办。 Python 函数似乎 运行 不像 JavaScript 函数,因为任何全局的东西都需要以某种方式返回。
- 您必须创建一个包含所需功能的单独文件 全球使用。
- 将此文件导入任何其他 python 文件,您应该可以开始了。
您是否有特定原因想要模仿确切的功能?如果不是这样就足够了。
我知道这是最糟糕的实现;),但我试图考虑其他可能性,它更接近问题中的 javascript 代码。
文件 1:setup_some_globals.py
num = 666
def g_alert_number():
global num
print num
def g_increase_number():
global num
num += 1
def g_set_number(x):
global num
num = x
变量 num 具有定义它的模块的范围。
文件 2:use_some.py
def use_global_functions():
from setup_some_globals import g_alert_number, g_increase_number, g_set_number
global g_alert_number
g_alert_number = g_alert_number
global g_increase_number
g_increase_number = g_increase_number
global g_set_number
g_set_number = g_set_number
use_global_functions()
g_alert_number()
g_increase_number()
g_alert_number()
g_set_number(99)
g_alert_number()
您无法访问这些功能,除非您调用 'use_global_functions()'
使用标准免责声明 请勿在实际代码中执行此操作,Python (3) 对您的 Javascript 的翻译如下:
def setup_some_globals():
# Local variable
num = 666
# You have to explicitly declare variables to be global,
# otherwise they are local.
global alert_number, increase_number, set_number
def alert_number():
# You can read a variable from an enclosing scope
# without doing anything special
print(num)
def increase_number():
# But if you want to assign to it, you need to be explicit about
# it. `nonlocal` means "in an enclosing scope, but not
# global".
nonlocal num
num += 1
def set_number(x):
# Same as above
nonlocal num
num = x
# Usage:
>>> setup_some_globals()
>>> set_number(3)
>>> increase_number()
>>> alert_number()
4
但如果您真的这样做了,那么几乎可以肯定有更好的方法来做您想做的事。