Comparisons and sorting of a numeric class....

Paul Rubin at nospam.invalid
Fri Jan 9 16:06:03 CET 2015

Chris Angelico <rosuav at> writes:
> for instance, I might have a socket object, and I might not, so I can
> use "if not self.socket: self.connect()" ...

This sounds like you want a Maybe or Option object.

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.  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()

More information about the Python-list mailing list