[Python-Dev] "and" and "or" operators in Py3.0

Skip Montanaro skip at pobox.com
Tue Sep 20 18:07:21 CEST 2005

Greg Ewing <greg.ewing <at> canterbury.ac.nz> writes:

> Raymond Hettinger wrote:
> > I propose that in Py3.0, the "and" and "or" operators be simplified to
> > always return a Boolean value instead of returning the last evaluated
> > argument.
> But then I would no longer be able to write
>    foo = something or default_value
> which is one of my favourite Pythonisms!

Same here.  I use this frequently.  In fact, I sometimes use it in preference
to a default param in {}.get():

    foo = somedict.get("blarg") or expensive_default()

That way the expensive default isn't calculated unless you need it.  (Of
course, the dict has to only store values that evaluate to non-False, which
might not be possible in all situations.)


More information about the Python-Dev mailing list