[Python-3000] else Versus or (Was: Re: default argument surprises)

Mike Meyer mwm at mired.org
Wed Aug 27 18:11:42 CEST 2008


On Wed, 27 Aug 2008 01:15:48 -0400 Eric Astor <eastor1 at swarthmore.edu> wrote:
> True, but 'else' can be a more specific operator, so that
> >>> arg = arg else bar
> is equivalent to:
> >>> if arg is None: arg = bar,

Yes, but only because you used arg twice. Dealing with the general
case clarifies things a little:

>>> arg = bar else foo
which is equivalent to
>>> arg = bar if bar is not None else foo

whereas your original will skip the assignment to arg if bar were not
None. Your proposed semantics have the "else" keyword change the
behavior of the assignment statement, which I suspect is unintended.

> where
> >>> arg = arg or bar
> is instead equivalent to:
> >>> if not arg: arg = b

Again, you've hidden part of the semantics by using arg twice. I
believe you want:

>>> arg = bar or foo
which is equivalent to
>>> arg = bar if bar else foo

In short, this change would only be useful in cases where a variable
has a default value of None, and you want to change that default value
to something other than None if it's not None. Which pretty much
limits it to dealing with arguments (otherwise we don't have a default
value) whose default value is a mutable object (otherwise we just use
the value we really want as the default) you're going to change
(otherwise, we just use the value we really want as the default) that
can evaluate to false under some conditions (otherwise, we could use
the "or" variant).

That seems to be a thin use case for a change that will generally only
affect a single line of code.

       <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org


More information about the Python-3000 mailing list