Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Proposal: function which simulates C ?: operator

Reply
Thread Tools

Proposal: function which simulates C ?: operator

 
 
Dan Bishop
Guest
Posts: n/a
 
      07-12-2004
"Mitja" <(E-Mail Removed)> wrote in message news:<LAbIc.6518$(E-Mail Removed)>...
> Adal Chiriliuc <(E-Mail Removed)>
> (news:(E-Mail Removed) ) wrote:
> > I think a function similar to the one below should be
> > added to the
> > builtin module:
> >
> > def boolselect(condition, trueresult, falseresult):
> > if condition:
> > return trueresult
> > else:
> > return falseresult
> >
> > --- WITH
> >
> > scalewidth = boolselect(self.__vertical, _scalew, _scaleh)
> >
> > --- WITHOUT
> >
> > if self.__vertical:
> > scalewidth = _scalew
> > else:
> > scalewidth = _scaleh

>
> > What do you think?

>
> Here's a nifty workaround I figured out myself but am sure others use it as
> well:
> scalewidth=(_scaleh,_scalew)[__self.vertical]


If you're going to use the "(F, T)[C]" syntax, it would be better to
write it as "(F, T)[bool(C)]" to ensure that the array index is 0 or
1.

However, a better alternative is "(C and [T] or [F])[0]", which
short-circuits and also has the advantage of having the same order as
"if".

If you're absolutely certain that T will never be false, you can
simplify this to "C and T or F".
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      07-12-2004
Dan Bishop wrote:

> However, a better alternative is "(C and [T] or [F])[0]", which
> short-circuits and also has the advantage of having the same order as
> "if".


It's got the same order, but it's completely opaque. It's hardly better
if you're interested in readability.

--
__ Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Performing in front of a live audience is like a feeling of shock.
-- Sade Adu
 
Reply With Quote
 
 
 
 
Reinhold Birkenfeld
Guest
Posts: n/a
 
      07-12-2004
Peter Hansen wrote:
> Adal Chiriliuc wrote:
>
>> I didn't know this was discussed so much in the past. I should have
>> searched before.

>
> And if you read all of it, you probably wouldn't be making
> the following suggestion, either.
>
>> I now suggest that this function be added without implying that it's
>> the Python equivalent of ?: and with the docs clearly explaining that
>> it's not ?: and how it differs.

>
> It won't happen because (a) it's a trivial function to write
> on-demand,


How do you write this function without lambda in the call?

> and (b) there are already several different ways of
> spelling it with current Python syntax if you don't want a
> function


One uglier than the other.

> and (c) I suspect that the requirement for short-circuit
> evaluation would actually be *more* common in Python than it
> appears it is in C++ based on your very limited sample population.


ACK.

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      07-12-2004
Reinhold Birkenfeld wrote:

> Peter Hansen wrote:
>>It won't happen because (a) it's a trivial function to write
>>on-demand,

>
> How do you write this function without lambda in the call?


Basically, you use if/then like you are "supposed" to.
But the OP wasn't asking about a short-circuit version, just
the trivial fully evaluated function approach.

>>and (b) there are already several different ways of
>>spelling it with current Python syntax if you don't want a
>>function

>
> One uglier than the other.


Agreed. Use if/then and save the sanity of your maintenance
programmers.

>>and (c) I suspect that the requirement for short-circuit
>>evaluation would actually be *more* common in Python than it
>>appears it is in C++ based on your very limited sample population.

>
> ACK.


Ironic that Bill the Cat changed the meaning of that from
positive to negative. (Or did he always use mixed-case,
so that all-caps ACK is still uniquely ASCII?)

-Peter
 
Reply With Quote
 
Reinhold Birkenfeld
Guest
Posts: n/a
 
      07-13-2004
Peter Hansen wrote:
> Reinhold Birkenfeld wrote:
>
>> Peter Hansen wrote:
>>>It won't happen because (a) it's a trivial function to write
>>>on-demand,

>>
>> How do you write this function without lambda in the call?

>
> Basically, you use if/then like you are "supposed" to.
> But the OP wasn't asking about a short-circuit version, just
> the trivial fully evaluated function approach.


Granted. This or (x and y or z) without y being false...

>>>and (b) there are already several different ways of
>>>spelling it with current Python syntax if you don't want a
>>>function

>>
>> One uglier than the other.

>
> Agreed. Use if/then and save the sanity of your maintenance
> programmers.
>
>>>and (c) I suspect that the requirement for short-circuit
>>>evaluation would actually be *more* common in Python than it
>>>appears it is in C++ based on your very limited sample population.

>>
>> ACK.

>
> Ironic that Bill the Cat changed the meaning of that from
> positive to negative. (Or did he always use mixed-case,
> so that all-caps ACK is still uniquely ASCII?)


Well, I don't know Bill the Cat, should I?

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      07-13-2004
Reinhold Birkenfeld wrote:

> Peter Hansen wrote:
>>Reinhold Birkenfeld wrote:
>>>ACK.

>>
>>Ironic that Bill the Cat changed the meaning of that from
>>positive to negative. (Or did he always use mixed-case,
>>so that all-caps ACK is still uniquely ASCII?)

>
> Well, I don't know Bill the Cat, should I?


Probably not. It was a comic strip called "Bloom County"
which featured among many other things a cat named Bill who
was, uh, just a tad incoherent, and given to mutterings like
"Ack! Pthft!"

See http://encyclopedia.thefreedictionar...ll%20the%20Cat
for background, and the following for a nice pic:
http://www.premier.net/~cspedale/opus/images/bill2.jpg

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
code simulates great, but error in synthesis digital pig VHDL 1 11-05-2007 08:59 PM
ValidationExpression that simulates the MaxLength property Nathan Sokalski ASP .Net 2 01-03-2007 07:30 AM
ValidationExpression that simulates the MaxLength property Nathan Sokalski ASP .Net Web Controls 2 01-03-2007 07:30 AM
website that simulates browsers? Woolly Mittens HTML 7 10-26-2003 10:22 AM
Greater than operator Vs Equal to operator : Which one is efficient? Vivek Mandava C Programming 28 09-11-2003 10:43 AM



Advertisments