Jul-06-2018, 03:32 PM
A useful addition to the python base is an if statement that can capture the expression's result in a variable if it's truthy:
but if the result of the expression is only valuable when it's not falsy then this syntax expresses it more cleanly.
An alternative pattern that would entirely be replaced:
if re.match(pattern, str) as match: print(match.group(0))Which could replace some current uses of this pattern:
match = re.match(patter, str) if match: print(match.group(0)) # damages readability if match is only needed inside the ifI know the
if
doesn't introduce a new scope (variables defined inside the if can be referenced outside),but if the result of the expression is only valuable when it's not falsy then this syntax expresses it more cleanly.
An alternative pattern that would entirely be replaced:
if re.match(pattern, str): match = re.match(patter, str) print(match.group(0)) # damages maintainability because any changes will need to be applied twice # also it's less efficientWould people be interested in this feature and is it PEP-able? I haven't found any PEP proposals related to this.