[英]Referring to Python console variable in function
I'm not sure about the scope rules of Python. 我不确定Python的范围规则。 If I'm using the Python console and define a variable, then import a module and call a function in that module, is there a way to refer to the variable within the function? 如果我使用Python控制台并定义一个变量,然后导入一个模块并在该模块中调用一个函数,是否可以在函数内引用该变量? For instance: 例如:
>> option = 2
>> import choosingModule
>> choosingModule.choosingFunction()
And then in choosingModule: 然后在选择模块时:
def choosingFunction():
if option == 0:
doThis()
elif option == 1:
doThat()
elif option == 2:
doTheOtherOne()
else:
doWhateverYouFeelLike()
Obviously that is a simple example. 显然,这是一个简单的例子。 The reason why I want to do this is that I have four different versions of a function and I want to be able to choose from the console which one should be used. 之所以要这样做,是因为我有一个函数的四个不同版本,并且希望能够从控制台中选择应该使用哪个版本。 The function is called fairly deep in to the code, so I don't want to have to pass a parameter through every single function call, especially as I only need to compare them at the testing stage; 该函数在代码中已被深深地调用,所以我不想在每个函数调用中都传递参数,尤其是在测试阶段,我只需要对它们进行比较; when this reaches the production version, only one of will be used. 当达到生产版本时,将仅使用其中之一。
So, is there any way to refer to a Python console variable in a called function? 那么,有什么方法可以在被调用函数中引用Python控制台变量? Or is there perhaps some other way to do what I need? 还是有其他方法可以满足我的需求?
When you run the REPL, you're the __main__
module, so you can import that and use the variables from it: 运行REPL时,您是__main__
模块,因此可以导入该模块并使用其中的变量:
def choosingFunction():
option = __import__('__main__').option
if option == 0:
doThis()
elif option == 1:
doThat()
elif option == 2:
doTheOtherOne()
else:
doWhateverYouFeelLike()
However, a better way to do this if you don't absolutely need the variable to be defined in the console is to set it on choosingModule
. 但是,如果您绝对不需要在控制台中定义变量,则更好的方法是将其设置为choosingModule
。 Then: 然后:
def choosingFunction():
global option
if option == 0:
doThis()
elif option == 1:
doThat()
elif option == 2:
doTheOtherOne()
else:
doWhateverYouFeelLike()
>>> import choosingModule
>>> choosingModule.option = 2
>>> choosingModule.choosingFunction()
However, you should only use either while testing. 但是,您仅应在测试时使用。 Please don't use these in production code. 请不要在生产代码中使用这些代码。
I'd recommend you to keep that variable in the global scope and access it using the global statement. 我建议您将该变量保留在全局范围内,并使用global语句访问它。
The keyword global allows you to reference a variable in the global namespace despite the existence of another variable with the same name on the function caller. 关键字global允许您在全局名称空间中引用变量,尽管函数调用程序上存在另一个具有相同名称的变量。
For example: this will print "tangerine" 例如:这将打印“ tangerine”
def foo():
def bar():
return fruit
fruit = "tangerine"
return bar2()
fruit = "banana"
print(foo()) # will print "tangerine"
But this will print "banana". 但这将打印“香蕉”。
def foo2():
def bar2():
global fruit # fetch me the fruit in the global scope
return fruit
fruit = "tangerine"
return bar2()
fruit = "banana"
print(foo2()) # will print "banana"
without the global keyword, the interpreter will follow the "LEGB rule" in order to find the variable you're trying to reference: 如果没有global关键字,解释器将遵循“ LEGB规则”以查找您要引用的变量:
If all of the searches above fail to find the variable name, python will raise a NameError Exception: 如果以上所有搜索均未能找到变量名称,则python将引发NameError异常:
$ NameError: name 'fruit' is not defined
Then you could do: 然后,您可以执行以下操作:
def choosingFunction():
global option
if option == 0:
doThis()
elif option == 1:
doThat()
elif option == 2:
doTheOtherOne()
else:
doWhateverYouFeelLike()
>> option = 2
>> import choosingModule
>> choosingModule.choosingFunction()
Keep in mind that relying on the existence of a global variable in the module that is calling yours is considered a very bad practice, and mustn't be used in anything serious. 请记住,依赖于调用您的模块中的全局变量的存在被认为是一种非常糟糕的做法,绝不能在任何严重的情况下使用。 ;) ;)
For more information on the global statement, check the documentation: Python 3.x and Python 2.7 . 有关全局语句的更多信息,请查看文档: Python 3.x和Python 2.7 。 And for info about the scopes and namespaces rules check here (Python 3.x) or here (Python 2.7). 有关范围和名称空间规则的信息,请在此处 (Python 3.x)或此处 (Python 2.7)进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.