Pythonic way for missing dict keys

Alex Martelli aleax at
Thu Aug 2 17:45:13 CEST 2007

Bruno Desthuilliers <bruno.42.desthuilliers at>

> Alex Popescu a écrit :
> > Bruno Desthuilliers <bdesth.quelquechose at> wrote in
> > news:46b0ed0b$0$2257$426a74cc at 
> (snip)
> >> if hasattr(obj, '__call__'):
> >>    # it's a callable
> >>
> >> but I don't find it so Pythonic to have to check for a __magic__
> >> method. 
> > 
> > It looks like Python devs have decided it is Pythonic, because it is
> > already in the PEP. 
> I do know, and I disagree with this decision.
> FWIW, repr(obj) is mostly syntactic sugar for obj.__repr__(), 
> getattr(obj, name) for obj.__getattr__(name), type(obj) for 
> obj.__class__  etc... IOW, we do have a lot of builtin functions that
> mostly encapsulate calls to __magic__ methods, and I definitively don't
> understand why this specific one (=> callable(obj)) should disappear. I

Maybe because it DOESN'T "encapsulate a call" to a magic method, but
rather the mere check for the presence of one?

> usually have lot of respect for Guido's talent as a language designer
> (obviously since Python is still MFL), but I have to say I find this 
> particular decision just plain stupid. Sorry.

The mere check of whether an object possesses some important special
method is best accomplished through the abstract-base-classes machinery
(new in Python 3.0: see <>).  At
this time there is no Callable ABC, but you're welcome to argue for it
on the python-3000 mailing list (please do check the archives and/or
check privately with the PEP owner first to avoid duplication).


More information about the Python-list mailing list