简体   繁体   中英

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:

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.

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.

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.

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. 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.

In one case, you're creating the variable, then modify it whatever the branch: 1 creation+assign, 1 assign (overwriting the old value completely).

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)

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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