[Python-ideas] Introduce collections.Reiterable

Stephen J. Turnbull stephen at xemacs.org
Sat Sep 21 21:04:39 CEST 2013


Ryan writes:

 > I can still see why checking if its callable is a good idea in some
 > cases.

You can always rewrite the LBYL in EAFP form:

    try:
        self.call[item]()
    except TypeError as e:
        raise CallbackError(...) from e

This is definitely preferred if you can enclose a whole suite in a try
and expect the CallbackError to be infrequent, eg:

    try:
        while item in input:
            self.call[item]()
    except TypeError as e:
        raise CallbackError(...) from e

However, what you probably really want to do (which is a better
argument for LBYL, anyway) is

    if callable(callback):
        self.call[item] = callback
    else:
        what_would_jruser_do()



More information about the Python-ideas mailing list