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.fl...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to