In Python 3, I have to check that 3 conditions are not happening in a function parameter, so, I created 3 functions:
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)
So, there are 3 functions, one of them iterating a string, an operation that is very heavy potentially. But, if one function returns already False, it is not necessary to check the remaining ones, we know the logical AND will return False, being able to make this less computationally expensive.
So, I am wondering if Python (CPython or other implementations) is optimizing this, therefore, the usage of return check_lenght(id_str) and check_format(id_str) and check_case_num(id_str)
is right, or if it would be better to check these functions one by one and returning False as soon as the first of them is False, having a more optimal but, maybe, less readable solution.
How this expression is being evaluated in Python?
I tried to find the answer to this question googling for it and also in this site