__contains__ vs. __getitem__

Alex Martelli aleax at mac.com
Fri Aug 11 01:45:06 EDT 2006


Bruno Desthuilliers <onurb at xiludom.gro> wrote:

> David Isaac wrote:
> >> Alan Isaac wrote:
> >>> I have a subclass of dict where __getitem__ returns None rather than
> >>> raising KeyError for missing keys.  (The why of that is not important
> > for
> >>> this question.)
> > 
> > "Bruno Desthuilliers" <onurb at xiludom.gro> wrote:
> >> Well, actually it may be important... What's so wrong with d.get('key')
> >> that you need this behaviour ?
> > 
> > I want to use the mapping with string interpolation.
> > 
> Well, this makes sens... But then why not use a plain dict to collect
> data, and wrap it in a special one just before using it for
> interpolation ? ie:

Using a single container (and being able to modify and use it fluidly)
is simply handier.  That's what defaultdict in Python 2.5 is for, btw.


Alex



More information about the Python-list mailing list