简体   繁体   English

从if语句嵌套在for循环中使变量成为全局变量

[英]Making variable global from if statement nested in for loop

I have a typical newbie problem of getting the result of a function into the global scope and I can usually follow how local and global variables work in simple examples but I can having a trouble understanding what happens when there are if statements nested in for loops. 我遇到了一个典型的新手问题,即将函数的结果放入全局范围,并且我通常可以在简单的示例中了解局部变量和全局变量的工作方式,但是在理解if语句嵌套在for循环中时会发生什么会遇到麻烦。

Below is the original code I am working with. 以下是我正在使用的原始代码。 I am trying to the the result of thisItem in to the global scope. 我正在尝试将thisItem的结果应用于全局范围。

def getTheFirstPoint(selection):
    for thisItem in selection:
        if type(thisItem) == GSNode:
            print 'LOCAL', thisItem
            return thisItem
    return None

I have been trying things like this: 我一直在尝试这样的事情:

thisItem = ''

def getTheFirstPoint(selection):
    global thisItem
    for thisItem in selection:
        if type(thisItem) == GSNode:
            print 'LOCAL', thisItem
            #return thisItem
    #return None

getTheFirstPoint(thisItem)
print 'GLOBAL:', thisItem

I've seen at times the global variable doesn't need to be explicitly set outside of the function – do I need the " thisItem = ' ' "? 我有时看到不需要在函数外部显式设置全局变量-我需要“ thisItem =''“吗?

Are the returns necessary? 是否需要退货?

What do I need to do to access thisItem globally? 我需要做什么才能全局访问thisItem?

Any help will be appreciated. 任何帮助将不胜感激。

If you run code: 如果您运行代码:

thiItem = ''

Inside function definition the new local variable will be created. 在函数定义内部,将创建新的局部变量。 If you do run code below in function 如果您确实在函数中运行以下代码

global thisItem
thisItem = ''

The global variable will be modified. 全局变量将被修改。 When you run for loop you create new local variable for [newVarName] in [iterable] . 运行for循环时,请for [newVarName] in [iterable]创建新的局部变量。 After you have defined thisItem as global do not use this name second time in the for loop. 将thisItem定义为全局后,请勿在for循环中第二次使用此名称。

If you wish to modify global variable on condition the code below will do the work: 如果您希望根据条件修改全局变量,则下面的代码将起作用:

thisItem = ''

def getTheFirstPoint(selection):
    global thisItem
    for item in selection:
        if type(item) == GSNode:
            print 'LOCAL', item
            # Modify global variable.
            thisItem = item
            #return thisItem
    #return None

getTheFirstPoint(thisItem)
print 'GLOBAL:', thisItem

PS I have a feeling that the Python Generators may be use full for you, also consider going more closely to the information about global vs local variables . PS:我觉得Python生成器可能已经为您所用,也可以考虑更深入地了解有关全局变量和局部变量的信息。

I've seen at times the global variable doesn't need to be explicitly set outside of the function – do I need the " thisItem = ' ' "? 我有时看到不需要在函数外部显式设置全局变量-我需要“ thisItem =''“吗? If you create a variable thisItem inside a function a local variable will be created in that function. 如果在函数内部创建变量thisItem,则将在该函数中创建局部变量。 If you use 如果您使用

previous_function():
    thisItem = ''
new_function():
    global thisItem 
    thisItem = 'updated value'

Then the value will be overwritten when you call it in new_function. 当您在new_function中调用该值时,该值将被覆盖。 That's why you need to define it outside any other function. 这就是为什么您需要在任何其他函数之外定义它的原因。

Are the returns necessary? 是否需要退货? No, the returns are not necessary. 不,不需要退货。 As you can call your global variable from any other function. 您可以从任何其他函数调用全局变量。 If you have another variable with the same name, you need to specify that you want to use the global definition and not the local one as in above example. 如果您有另一个具有相同名称的变量,则需要指定要使用全局定义而不是本地定义,如上例所示。

What do I need to do to access thisItem globally? 我需要做什么才能全局访问thisItem? You can use it like below: 您可以如下使用它:

thisItem = ''
def getTheFirstPoint(selection):
    for thisItem in selection:
        if type(thisItem) == GSNode:
            print 'GLOBAL', thisItem
def some_new_function():
    global thisItem
    thisItem = 'modified value'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM