"?:", "a and b or c" or "iif"

Gordon McMillan gmcm at hypernet.com
Fri May 28 12:35:22 EDT 1999


Andrew Clover wrote:

> Robert Meegan (Robert.Meegan at wcom.com) wrote:
> 
> > I still don't understand what so many people have against writing simple
> > if/else statements.
> 
>  Although that's fine in this case, where the result of the
>  expression is
> assigned to a variable, that doesn't always happen. For example:
> 
>   selectFlavour(nutAllergy ? strawberry : pistachio)
> 
>  Would have to be written as:
> 
>   if nutAllergy:
>     flavour= strawberry
>   else:
>     flavour= pistachio
>   selectFlavour(flavour)
> 
>  It think the first version is a lot better, the C-ish ugliness of
>  the symbols
> '?' and ':' notwithstanding. Part of the reason is simply that I
> detest temporary variables.

No no no. You _love_ temporary variables. You hate _explicit_ 
temporary variables. You want the language to do it for you.

I've had to get used to this. The Python compiler makes very very few 
assumptions. Guido anticipated the likes of Tim, Steven, and even 
Neel (though no one expected Michael Hudson). So names are looked 
up each and every time. To get efficiency, you do better to use 
_lots_ of temporary variables. This puts everything in the local 
namespace, which is the first place Python will look.

Just pretend you're dealing with a 1.x release of MSVC, where you 
could never turn on optimization, because nothing worked when you did 
<0.999 wink>.


- Gordon




More information about the Python-list mailing list