Is there any plan to include inline conditionals in Python? For example:
def isNegative(x):
return x < 0 ? True : False
Thanks!
-jag
--
Joshua Ginsberg <jo***@brainsto rminternet.net>
Brainstorm Internet Network Operations 15 2581
"Joshua Ginsberg" <jo***@brainsto rminternet.net> wrote in message
news:ma******** *************** *************** @python.org... Is there any plan to include inline conditionals in Python? For example:
def isNegative(x): return x < 0 ? True : False
Thanks!
-jag
-- Joshua Ginsberg <jo***@brainsto rminternet.net> Brainstorm Internet Network Operations
How about something like: def iif(condition, true=True, false=False):
.... if condition:
.... return true
.... return false
.... iif('foo' == 'bar', 'w00t', 'l33t')
'l33t' iif('bar' == 'bar', 'w00t', 'l33t')
'w00t' iif('bar' == 'bar')
True iif('foo' == 'bar')
False
Hope this helps.
Adonis
Adonis wrote: How about something like:
def iif(condition, true=True, false=False):
... if condition: ... return true ... return false ...
Just a thought, but this would have unexpected results if used with
function calls, if those function calls had side-effects, since both are
evaluated, whereas the ternary operator in other languages only
evaluates one of the possible expressions.
Joshua Ginsberg <jo***@brainsto rminternet.net> writes: Is there any plan to include inline conditionals in Python? For example:
def isNegative(x): return x < 0 ? True : False
Read PEP 308, and note that this was probably the largest flamewar in
Python history (well, before the still-ongoing decorators
discussion...).
But to answer your question: no.
Cheers,
mwh
--
Linux: Horse. Like a wild horse, fun to ride. Also prone to
throwing you and stamping you into the ground because it doesn't
like your socks. -- Jim's pedigree of operating systems, asr
Peter Hansen <pe***@engcorp. com> wrote: Joshua Ginsberg wrote:
Is there any plan to include inline conditionals in Python? For example:
def isNegative(x): return x < 0 ? True : False
This is a FAQ:
Newbies, please consider reading the several FAQs that you will find at http://www.python.org/doc/faq/ before posting questions which might be answered there (i.e. just about anything).
Peter's right. And Joshua's example shows how wise the BDFL was in
ruling out ternaries: sure, good programmers might occasionally have
found good uses for them, but we' have paid that with a LOT of horrid
code like that -- I've seen lots like that in C & its ilk, too.
return x < 0
on its own does EXACTLY the same job as the requested/wished-for
return x < 0 ? True : False
except is cleaner, clearer, faster, more concise, easier to read. So,
having a ternary operator would enable a little programming horror that
the lack of a ternary operator dissuades a little.
Of course, absit iniuria verbis, one cannot make anything foolproof,
because fools are SO ingenious -- I've also seen lots of code like:
if x < 0:
return True
else:
return False
which is, si licet es, even _worse_ than the wished-for ternary use!-)
Alex
Op 2004-08-26, Alex Martelli schreef <al*****@yahoo. com>: Peter Hansen <pe***@engcorp. com> wrote:
Joshua Ginsberg wrote:
> Is there any plan to include inline conditionals in Python? For example: > > def isNegative(x): > return x < 0 ? True : False
This is a FAQ:
Newbies, please consider reading the several FAQs that you will find at http://www.python.org/doc/faq/ before posting questions which might be answered there (i.e. just about anything).
Peter's right. And Joshua's example shows how wise the BDFL was in ruling out ternaries: sure, good programmers might occasionally have found good uses for them, but we' have paid that with a LOT of horrid code like that -- I've seen lots like that in C & its ilk, too.
When using list comprehension not having a ternary operator can be
a PITA. It is of course possible I miss something but how am I
supposed to do the following:
[ x.property ? foo(x) : bar(x) for x in Somelist ]
--
Antoon Pardon
Antoon Pardon <ap*****@forel. vub.ac.be> wrote: Op 2004-08-26, Alex Martelli schreef <al*****@yahoo. com>:
.... Peter's right. And Joshua's example shows how wise the BDFL was in ruling out ternaries: sure, good programmers might occasionally have found good uses for them, but we' have paid that with a LOT of horrid code like that -- I've seen lots like that in C & its ilk, too.
When using list comprehension not having a ternary operator can be a PITA. It is of course possible I miss something but how am I supposed to do the following:
[ x.property ? foo(x) : bar(x) for x in Somelist ]
def foo_or_bar(x):
if x.property:
return foo(x)
return bar(x)
[foo_or_bar(x) for x in Somelist]
(still, I'd like to see a ternary conditional operator too...) al*****@yahoo.c om (Alex Martelli) writes:
[...] Of course, absit iniuria verbis, one cannot make anything foolproof, because fools are SO ingenious -- I've also seen lots of code like:
if x < 0: return True else: return False
which is, si licet es, even _worse_ than the wished-for ternary use!-)
[...]
Argh.
I looked at that, and had a sinking feeling. I just found about ten
instances of that in my code!
Strange that, though I was innoculated against the general case of
this disease in the some years ago, I was apparently still entirely
susceptible to this particular strain of it.
John
Antoon Pardon <ap*****@forel. vub.ac.be> wrote:
... When using list comprehension not having a ternary operator can be a PITA. It is of course possible I miss something but how am I supposed to do the following:
[ x.property ? foo(x) : bar(x) for x in Somelist ]
If you HAVE to use an LC by doctor's order, the above effect might be
obtained by coding something like:
[ (bar,foo)[bool(x.property )](x) for x in Somelist ]
If your physician should relent and let you code normal Python, though,
aux = []
for x in Somelist:
if x.property:
aux.append(foo( x))
else
aux.append(foo( x))
would be vastly more readable; "sparse is better than dense" and any LC
is far too dense to be Pythonic here.
Alex This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: FHuang |
last post by:
Ok, I'm having some trouble with conditionals, for some reason PHP is screwing them up.
First off, here is the code:
<?php
$username = "fred";
$userdata = "./$username.txt";
$fp = fopen("$userdata", "r", 1);
$origpass = fread($fp, 5);
if($passcount = 1)
{
|
by: Steven T. Hatton |
last post by:
I don't really understand the details of what happens when a compiler
inlines a given function. I know that basically it's supposed to cause the
function to be placed directly on the stack per invocation (or something
like that.) That would be in contrast to loading one instance of the
function and branching to it each time.
I don't believe that means I would have a separate instance of a member
function loaded per object instance. But...
|
by: Paul Dale |
last post by:
Hi All,
I know that several of you will probably want to reply "you should write
a parser", and I may. For that matter any tips on theory in that
direction would be appreciated.
However, if you would indulge me in my regex question I would also be
most grateful.
I'm writing an edi parser and to be in compliance with the specification
|
by: jdwyer3 |
last post by:
I'm using Visual C++.Net 2003, and I have noticed a little quirkiness
when using a conditional based on the comparison of two calculations
that are performed inline with the conditional.
For example, if I run this code:
double angle1;
double angle2;
angle1 = 45;
|
by: steven |
last post by:
Is it possible to combine conditionals to call out data?
I have a list of records that include a date. I can call out all dates
after the date after tomorrow:
IIf(Date()+1)<,...,...
And I can call dates before next week:
IIf(< Date()+7,...,...
But I'd like to isolate those that fall between the day after tomorrow
and next week. I was thinking that this would work, but it doesn't:
| |
by: Leon Lambert |
last post by:
I would appreciate it if someone could help me understand NaN handling
with respect to conditionals in IL code. I am playing with a small IL
interpreter and having a little problem with it. Following is a small
piece of C# code that gets compiled by VS2005 to simulate my problem.
static void Main(string args)
{
Double db1 = 1.0;
Double db2 = Double.NaN;
if (db1 <= db2)
db2 = 1.0;
|
by: .Net Sports |
last post by:
I need to convert some C# ?Conditionals over to vb.net (most likely in
vb.net using If..Then statements), but the C#2VBNETconverter by
KamalPatel isn't converting them:
string PurchaseType = (Convert.ToString(drwData) ==
"True") ? "ID" : "PID";
XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
"Highlight" : "Selection");
|
by: Hul Tytus |
last post by:
comp.lang.c
c programs & shell conditionals
How is a unix shell script made to respond to the value returned
by a program compiled from c code?
The shell script below is my current effort, which doesn't work.
The c code shows what was used to generate a.out, seen in the shell script.
The value returned was varied from 0 to 1 for testing.
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
| |
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |