Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在条件语句中声明变量是否有问题?_Python_Conditional Statements - Fatal编程技术网

Python 在条件语句中声明变量是否有问题?

Python 在条件语句中声明变量是否有问题?,python,conditional-statements,Python,Conditional Statements,在条件变量的所有可能分支中重新定义变量之前,定义变量是否可以防止出现问题 例如,此代码应: # Condition could fail try: textureIndices = someExpression() # textureIndices is defined here if it does except: textureIndices = [] return textureIndices 改写如下:

在条件变量的所有可能分支中重新定义变量之前,定义变量是否可以防止出现问题

例如,此代码应:

    # Condition could fail
    try:
        textureIndices = someExpression()

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

    return textureIndices
改写如下:

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

    try:
        textureIndices = someExpression()


    except:
        textureIndices = 66

    return textureIndices
或者,由于除之外的
打开了其他问题,此处的
纹理索引的定义是否存在问题:

if condition:
    textureIndices = someExpression()

else:
    textureIndices = 66

return textureIndices 
减少问题

唯一的区别在于第二个版本
textureIndicates
是在条件之外定义的

我不明白为什么这很重要,因为
textureIndices
不可能在条件中被赋值,但我可以理解为什么从内务管理的角度来看,知道变量被赋值是件好事

例如,如果在第一个示例中除了
语句之外没有
,则不会总是定义
纹理索引
,并且
返回
将导致错误

但是,如果不向前定义在条件变量的两个原因中定义的变量,是否会出现问题?

创建变量时会修改变量字典(
局部变量
全局变量

在一种情况下,您正在创建变量,然后修改它,不管分支是什么:1 creation+assign,1 assign(完全覆盖旧值)

在省略预先创建的情况下,只有1个creation+assign,因此从技术上讲,不在分支之前声明它会更快(少一个字典查找,少一个无用的赋值)


除了帮助PythonIDE完成分支中的变量名之外,我认为前面的声明在这种情况下是无用的,甚至是麻烦的,因为这两个分支都被覆盖了(可能是一个旧的编译语言编程反射)。它可能感兴趣的唯一情况是一组复杂的分支,您只在几个分支中设置变量。这里不是这样。

一个原因是它创建了冗余代码。在本例中,这似乎不是很明显,但以一个示例为例,其中有多个unique except语句捕获代码中的多个异常。想象一下,如果有人想要重构您的代码或添加额外的except语句

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
如果有多个变量以这种方式运行,您可以看到这种情况是如何快速升级的。通过首先声明基本情况,可以减少冗余

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

在第一种情况下,使用
except
而不是
except SomeError
。由于您不应该将所有错误都包括在一起,因此在良好实践中,您的变量可能不存在,除非您提前定义它。“应该重写”的来源是什么?编译器和运行时系统对此没有问题。这里不是这样,但通常情况下,您不应该详细说明捕获到的错误吗?即使您只捕获了2个特定错误,其余的则会使脚本崩溃,您也必须定义两次默认值,这不会使DRY(以一种非常小的方式)失败吗?不知道您在说什么。当然,没有异常类型的try/except(甚至
异常
)是不好的做法。它甚至捕捉到CTRL+C。但这似乎与问题无关。@roganjosh我当然不是在暗示这一点!否决票消失了。。。好的,很好。现在我永远也不知道为什么:)@roganjosh当我们在批评操作码时,
textureIndices=[func()]中的物对物(
对于
textureIndices=list(func())
:)批评可能是个更好的词:)我认为这是个好问题,只是这个例子并没有很好地说明这种情况,在
中定义事物是一个坏主意,除了
,它只会突出理解上的差距,而且我年龄很大:)