Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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_Python 3.x_Optimization_Evaluation - Fatal编程技术网

Python:优化和评估

Python:优化和评估,python,python-3.x,optimization,evaluation,Python,Python 3.x,Optimization,Evaluation,在Python 3中,我必须检查函数参数中没有发生3个条件,因此,我创建了3个函数: def check_lenght(string_id): # if length is right, return True, otherwise, False def check_format(string_id): # return True if some internal format has to be used, in another case, False def check_

在Python 3中,我必须检查函数参数中没有发生3个条件,因此,我创建了3个函数:

def check_lenght(string_id):
    # if length is right, return True, otherwise, False

def check_format(string_id):
    # return True if some internal format has to be used, in another case, False

def check_case_num(string_id):
    # manual iteration to check if certain characters are not contained, return True, otherwise, False


def valid_product_code(id_str):
    return check_lenght(id_str) and check_format(id_str) and check_case_num(id_str)
因此,有3个函数,其中一个迭代字符串,这是一个可能非常繁重的操作。 但是,如果一个函数已经返回False,则不必检查其余的函数,我们知道逻辑结果,并将返回False,这样可以降低计算成本

因此,我想知道Python(CPython或其他实现)是否在优化它,因此,
返回check_lengh(id_str)、check_format(id_str)和check_case_num(id_str)
的使用是否正确,或者最好逐个检查这些函数,并在第一个函数为False时立即返回False,有一个更优化但可能可读性较差的解决方案

如何在Python中计算此表达式


我试图通过谷歌搜索找到这个问题的答案,在这个网站上,它被称为短路,是的,Python确实支持它:


如果
a
已返回
False
,则a、b和c
将不会计算
b
c

如果
x
已返回
True
,则
x或y
将不会计算
y


你会发现,这就是bool操作符在几乎所有类似C语言中的工作方式。

不可避免地与hmm类似。当然,以前也有人问过这个问题,也有人回答过这个问题。欢迎来到SOT,诀窍是知道在哪里寻找复制品。感谢并为复制品感到抱歉,我在谷歌和这里搜索,但我找不到答案,听起来不正确。“也许我误解了你的意思,”物理学家说,“这是短路定义的一部分。”<如果
a
已经是
False
,则不会对code>b进行评估。啊,我想我理解这个混淆,为了清楚起见进行了编辑。谢谢。投票被推翻了。