[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-3000
Subject:    Re: [Python-3000] else Versus or (Was: Re: default argument
From:       Mike Meyer <mwm () mired ! org>
Date:       2008-08-27 16:11:42
Message-ID: 20080827121142.42eb8cbd () mbook ! mired ! org
[Download RAW message or body]

On Wed, 27 Aug 2008 01:15:48 -0400 Eric Astor <eastor1@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@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
_______________________________________________
Python-3000 mailing list
Python-3000@python.org
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: http://mail.python.org/mailman/options/python-3000/python-3000%40progressive-comp.com
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic