Comparisons and sorting of a numeric class....

Marko Rauhamaa marko at pacujo.net
Fri Jan 9 10:16:46 EST 2015


Paul Rubin <no.email at nospam.invalid>:

> Marko's suggestion
>
>     rv = f()
>     if rv is not None:
>         return rv
>     rv = g()
>     if rv is not None:
>         return rv
>     return h()
>
> seems unspeakably ugly.

Well, "unspeakably" is exaggeration IMO. It is a bit lengthy but it is
crystal clear. While the "or" pattern is neat, I wouldn't make too much
of it.

> Rather than None on failure maybe f() and g() could return an empty
> list on failure, or a one-element list containing the item on success.
> That uses lists to simulate a Maybe type.
>
> Then the above could go (Python 3, untested):
>
>   def tries():
>     yield from f()
>     yield from g()
>     yield [h()]
>   return tries().next()

I think the medicine in this case is worse than the disease.


Marko



More information about the Python-list mailing list