473,789 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proposal: function which simulates C ?: operator

I think a function similar to the one below should be added to the
builtin module:

def boolselect(cond ition, trueresult, falseresult):
if condition:
return trueresult
else:
return falseresult

--- WITH

scalewidth = boolselect(self .__vertical, _scalew, _scaleh)

a1 = 0
a4 = boolselect(self .__vertical, self.__width, self.__height)
a2 = (a4 - scalewidth) // 2
a3 = a2 + scalewidth

self.__meterwid th = self.__meterrec t[0].Width()
self.__meterhei ght = self.__meterrec t[0].Height()
self.__meterlen gth = boolselect(self .__vertical, self.__meterhei ght, self.__meterwid th)

t = boolselect(self .__range >= 18, -18, -12)
redzone = self.__dBToOffs et(-6)
yellowzone = self.__dBToOffs et(t)

--- WITHOUT

if self.__vertical :
scalewidth = _scalew
else:
scalewidth = _scaleh

a1 = 0
if self.__vertical :
a4 = self.__width
else:
a4 = self.__height
a2 = (a4 - scalewidth) // 2
a3 = a2 + scalewidth

self.__meterwid th = self.__meterrec t[0].Width()
self.__meterhei ght = self.__meterrec t[0].Height()
if self.__vertical :
self.__meterlen gth = self.__meterhei ght
else:
self.__meterlen gth = self.__meterwid th

if self.__range >= 18:
t = -18
else:
t = -12
redzone = self.__dBToOffs et(-6)
yellowzone = self.__dBToOffs et(t)

---

What do you think?

PS: maybe a better name than boolselect could be found.

Jul 18 '05
15 1488
"Mitja" <nu*@example.co m> wrote in message news:<LA******* ***********@new s.siol.net>...
Adal Chiriliuc <me@spammers.co m>
(news:13******* *************** *@smtp.myrealbo x.com) wrote:
I think a function similar to the one below should be
added to the
builtin module:

def boolselect(cond ition, 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=(_sc aleh,_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".
Jul 18 '05 #11
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 && ma*@alcyone.com && 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
Jul 18 '05 #12
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 Linuxdistributi on 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
Jul 18 '05 #13
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
Jul 18 '05 #14
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 Linuxdistributi on 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
Jul 18 '05 #15
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
Jul 18 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1708
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason to accept it would be to pacify the supporters of the proposal, and that just isn't a good enough reason in language design. However, it got pretty darn close! I'm impressed with how the community managed to pull together and face the enormous...
31
2443
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based Keyword Arguments ----------------------------- Passing complicated arguments to functions is currently awkward in Python. For example, the typical way to define a class property winds up polluting the class's namespace with the property's get/set...
59
3949
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a candidate for Python 3000 yet? Chris
4
2737
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from the start that my knowledge of compiler implementation is very limited. Therefore, my suggestions may have to be rejected because they are difficult or impossible to implement. The proposal is based on the concept of "type similarity". Type...
17
2445
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The specific example was to define an "outer product" operator for matrices. (There was even a PEP, number 211, about this.) I gave it some thought, and Googled for previous discussions about this, and came up with this suggestion: User-defined...
23
5337
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes I applied myself to this problem and come up with this off-the-wall proposal for you people. Perhaps this idea has been proposed before, I don't know. The solutions I have seen all assume that the lambda must be completely inlined within the...
34
1884
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like elementtree and xist. I dont want to replace the packages but the packages could be used with the new operators and the resulting IMHO is much more readable. The syntax i would like is something like the below:
10
3312
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add signed/unsigned modifier to class declarations to next revision of C++ programming language
0
1667
by: terminator | last post by:
first: I find the 'r/l-value reference' terminology rather confusing and I find the following names for this new refrence type more suitable: temporary reference: Since it is generally intended to refere to temporary objects Immidiate referenc: Since literals are assumed as temporary in C++ and immidiate or quick in assembly language.
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.