Common Python Idioms

Stephen Eilert spedrosa at gmail.com
Thu Dec 7 08:54:55 EST 2006


Duncan Booth escreveu:

>
> >
> > Is there a list somewhere listing those not-so-obvious-idioms? I've
> > seen some in this thread (like the replacement for .startswith).
> >
> The release notes for each new version. Unfortunately the rest of the
> documentation sometimes lags behind the release notes.

Right. If and when time allows, I'll try to compile a list.

>
>
> > I do think that, if it is faster, Python should translate
> > "x.has_key(y)" to "y in x".
>
> Python doesn't know whether x has a __contains__ method until it tries to
> call it. In particular there may be user-defined dictionary-like objects in
> your program which support has_key but not 'in'. e.g. in at least some
> versions of Zope HTTPRequest objects support has_key but not __contains__.

You're right. Brain fart.


Stephen




More information about the Python-list mailing list