[Python-Dev] Call for prudence about PEP-572
Anthony Flury
anthony.flury at btinternet.com
Mon Jul 9 18:26:25 EDT 2018
On 09/07/18 08:26, Matěj Cepl wrote:
> On 2018-07-07, 15:48 GMT, Guido van Rossum wrote:
>> if validate(name := re.search(pattern, line).group(1)):
>> return name
> Except there is no error handling for situations when
> re.search() returns None, so one shouldn't use it anyway (most
> of the time). Which seems to me like another nice example why
> one should stay away from this style as much as possible. I am
> too lazy to be tempted into this
> nice-example-terrible-production-code world.
So wrap it in a try/except to capture the re.search returning None:
try:
if validate(name := re.search(pattern, line).group(1)):
return name
except TypeError:
# No match case
And this solution is even better if the validate function raises an
exception rather than True/False
which I think is much better than trying to do multiple steps :
match = re.search(pattern, line)
if match is not None:
name = match.group(1)
if name and validate(name):
return name
else:
# No valid case
else:
# No Match case
> Best,
>
> Matěj
--
--
Anthony Flury
email : *Anthony.flury at btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*
More information about the Python-Dev
mailing list