简体   繁体   English

在条件句中声明变量是否存在问题?

[英]Are there problems declaring variables in conditionals?

Does it prevent problems to define a variable before it is re-defined in all possible branches of a conditional? 在条件的所有可能分支中重新定义变量之前,它是否可以防止定义变量?

For instance should this code: 比如应该这个代码:

    # Condition could fail
    try:
        textureIndices = someExpression()

    # textureIndices is defined here if it does
    except:
        textureIndices = []

    return textureIndices

Be re-written as this: 重写为:

    # textureIndices is defined early and then re-defined in the conditional
    textureIndices = None

    try:
        textureIndices = someExpression()


    except:
        textureIndices = 66

    return textureIndices

Or, because except opens other issues, is there a problem with the definition of textureIndices here: 或者,因为except打开其他问题,这里的textureIndices的定义是否存在问题:

if condition:
    textureIndices = someExpression()

else:
    textureIndices = 66

return textureIndices 

to reduce problems? 减少问题?

The only difference is in the second version textureIndices is defined outside of the conditional. 唯一的区别是在第二个版本中textureIndices是在条件之外定义的。

I do not see why it matters because it is impossible for textureIndices to not be assigned a value in the conditional but I can see why from a housekeeping perspective it is nice to know the variable is assigned to something. 我不明白为什么它很重要因为textureIndices不可能在textureIndices中没有赋值,但我可以看到为什么从管家的角度来看,知道变量被分配给某些东西是件好事。

For instance if there was no except statement in the first example, then textureIndices wouldn't always be defined and return would cause an error. 例如, 如果第一个示例中没有except语句,则不会始终定义textureIndices并且return会导致错误。

However, are there problems if one does not forward define variables that are defined in both causes of a conditional? 但是,如果没有转发定义在条件的两个原因中定义的变量,是否存在问题?

One reason is it creates redundant code. 一个原因是它创建了冗余代码。 It doesn't seem very apparent in this case, but take an example where you have multiple unique except statements catching multiple exceptions in your code. 在这种情况下,它似乎不是很明显,但举一个例子,你有多个唯一的except语句在代码中捕获多个异常。 Imagine if someone wanted to refactor your code or add additional except statements. 想象一下,如果有人想重构您的代码或添加其他除语句。

textureIndices = None

try :
    textureIndices = [thing for thing in func()]fail

except InvalidTextException:
    textureIndices = []
    #lines to handle specific exception
except ValueError:
     textureIndices = []
     #lines to handle specific exception
except OSError:
     textureIndices = []
     #lines to handle specific exception

return textureIndices

If you had multiple variables that behaved this way, you can see how this quickly escalates. 如果您有多个以这种方式运行的变量,您可以看到它如何快速升级。 By declaring the base case first, you reduce redundancy. 通过首先声明基本情况,可以减少冗余。

textureIndices = []

try :
    textureIndices = [thing for thing in func()]fail

except InvalidTextException:
    #lines to handle specific exception
except ValueError:
     #lines to handle specific exception
except OSError:
     #lines to handle specific exception

return textureIndices

The variable dictionary ( locals or globals depending on the scope), is modified when you create a variable. 可变字典( localsglobals取决于范围),当创建一个变量被修改。

In one case, you're creating the variable, then modify it whatever the branch: 1 creation+assign, 1 assign (overwriting the old value completely). 在一种情况下,您正在创建变量,然后在分支中修改它:1创建+分配,1分配(完全覆盖旧值)。

In the case where you omit the beforehand creation, you only have 1 creation+assign, so technically it's faster not to declare it prior to the branch (one less dictionary lookup, one less useless assignment) 在省略预先创建的情况下,您只有1个创建+赋值,因此从技术上讲, 不要在分支之前声明它更快(少一个字典查找,少一个无用的赋值)

Apart from helping the Python IDE to complete the variable names in branches, I would say that the prior declaration is useless and even cumbersome in that case since both branches are covered (maybe an old compiled language programming reflex). 除了帮助Python IDE完成分支中的变量名称之外,我会说先前的声明在这种情况下是无用的甚至是麻烦的,因为两个分支都被覆盖(可能是旧的编译语言编程反射)。 The only case where it could have an interest would be a complex set of branches where you only set the variable in a few branches. 它可能有兴趣的唯一情况是一组复杂的分支,你只在几个分支中设置变量。 Ain't the case here. 不是这里的情况。

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

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