Function to determine list max without itertools

Michael Torrie torriem at gmail.com
Sat Apr 20 00:01:30 EDT 2019


On 04/19/2019 04:01 AM, Sayth Renshaw wrote:
> def max_try(listarg):
>     myMax = listarg[0]
>     try:
>         for item in listarg:
>             if item > myMax:
>                 myMax = item
>     except TypeError:
>         print(f'Only numbers are supported, this entry "{item}" was not')
>         pass
> 
>     return myMax

If I were you I wouldn't catch that exception.  The reason is that a
non-number is something your code just can't handle correctly anyway, so
better to let that original exception bubble up to the caller who can
deal with it.  By catching this exception, your code will fail, but
still return as if it succeeded.

Others may disagree. But I rarely catch exceptions in my code unless my
code specifically wants or needs to deal with them.



More information about the Python-list mailing list