473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inline Conditionals?

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***@brainstorminternet.net>
Brainstorm Internet Network Operations

Jul 18 '05 #1
15 2560
"Joshua Ginsberg" <jo***@brainstorminternet.net> wrote in message
news:ma**************************************@pyth on.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***@brainstorminternet.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
Jul 18 '05 #2
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.
Jul 18 '05 #3
Joshua Ginsberg <jo***@brainstorminternet.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
Jul 18 '05 #4
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:
http://www.python.org/doc/faq/progra...rnary-operator

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
Jul 18 '05 #5
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
Jul 18 '05 #6
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
Jul 18 '05 #7
JCM
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...)
Jul 18 '05 #8
al*****@yahoo.com (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
Jul 18 '05 #9
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
Jul 18 '05 #10
al*****@yahoo.com (Alex Martelli) writes:
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.


Hmm,

[ x.property ? foo(x) : bar(x) for x in Somelist ]

doesn't seem too dense, unless you consider -every- LC to be too
dense, in which case why have them? The LC seems to me to be both
more readable and harder to get wrong, unlike your sparse example,
which has 'foo' on both branches of the conditional where you meant
'foo' and 'bar'.

I could see adding some parentheses in the LC to improve readability:

[ (x.property ? foo(x) : bar(x)) for x in Somelist ]

but it doesn't seem like a big deal.
Jul 18 '05 #11
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
al*****@yahoo.com (Alex Martelli) writes:
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.
Hmm,

[ x.property ? foo(x) : bar(x) for x in Somelist ]

doesn't seem too dense, unless you consider -every- LC to be too
dense, in which case why have them?


I don't, but I do consider almost every application of ternary to result
in too-dense code, which is why I'm ecstatic not to have them.
more readable and harder to get wrong, unlike your sparse example,
which has 'foo' on both branches of the conditional where you meant
'foo' and 'bar'.


....as several people already noticed, proving the great readability of
the sparse way of expressing oneself...
Alex
Jul 18 '05 #12
Michael Hudson <mw*@python.net> wrote:
Joshua Ginsberg <jo***@brainstorminternet.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


BTW, is there ever going to exist the promised summary of the reasons
the PEP was rejected despite a 4:1 vote favoring it? It was a fine bit
of sophistry for the BDFL (or whoever ran the thing) to split the
supporting votes so that it looked like there was a lack of consensus
on substantial issues rather than the superficial difference of taste
that actually made the discussion so... active. I've been wondering
ever since how that would be spun in the final apologia.

--
The dualist evades the frame problem - but only because
dualism draws the veil of mystery and obfuscation
over all the tough how-questions -- Daniel C. Dennett
Jul 18 '05 #13
Alex Martelli <al*****@yahoo.com> wrote:
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
more readable and harder to get wrong, unlike your sparse example,
which has 'foo' on both branches of the conditional where you meant
'foo' and 'bar'.
....as several people already noticed, proving the great readability of
the sparse way of expressing oneself...


Amazing. If a typo slips into something you dislike then you cite that
as proof of how bad it is. When it happens to you while showing off
your preferred form, it becomes, by prestidigitation, proof of how good
that is. Clearly this is determined by nothing at all like logic.

--
Their arguments, being based not in reason, are immune to it.

Jul 18 '05 #14
Martin Maney <ma***@pobox.com> wrote:
Alex Martelli <al*****@yahoo.com> wrote:
Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
more readable and harder to get wrong, unlike your sparse example,
which has 'foo' on both branches of the conditional where you meant
'foo' and 'bar'.

....as several people already noticed, proving the great readability of
the sparse way of expressing oneself...


Amazing. If a typo slips into something you dislike then you cite that
as proof of how bad it is. When it happens to you while showing off
your preferred form, it becomes, by prestidigitation, proof of how good
that is. Clearly this is determined by nothing at all like logic.


It's not the typo that "becomes proof" (or, rather, indication) of the
higher readability of sparse expression: it's the fact that so many
readers noticed it and let me know, privately or publically. Why do you
think it's illogical to observe that? Typos and other errors are easier
to see when they're part of very readable code than when they're part of
dense, obfuscated code -- seems perfectly logical to me.

Anything that requires the same name (or whatever) to be entered more
than once can be subject to more typos than a form in which you enter
the information only once -- ameliorating which is a better chance for a
human reader to catch a typo due to the redundancy (while if the info is
entered only once there is no such redundancy and any catch needs to be
based on deeper understanding). This might be relevant, say, when
discussing <name> += <value> vs <name> = <name> + <value> -- when <name>
is sufficiently complicated the second form may have such issues which
the first one avoids, and there's no real chance of introducing a short,
simple temporary name to ameliorate things. This is not quite germane
to the discussion at hand, though.
Alex
Jul 18 '05 #15
Alex Martelli <al*****@yahoo.com> wrote:
It's not the typo that "becomes proof" (or, rather, indication) of the
higher readability of sparse expression: it's the fact that so many
readers noticed it and let me know, privately or publically. Why do you


Okay, let me unpack the thought more fully (as I should have done since
I was jumping on a weeks-old thread).

There's a recurring argument, seen most often when dissing a usage one
disagrees with (you're not by a long shot the only one who uses this,
BTW). One grabs typos in ones opponents' examples and construes them
as evidence of the inferiority of whatever it is one dislikes about
their example, usually a stylistic quirk or a proposed change. This is
a dubious argument on the face of it because it is so clearly
selective: when a typo occurs in a stylistically orthodox chunk of
code, it is nearly always regarde as being just a typo, not evidence of
some deeper problem with the style. To properly ground this argument
one would need to be an honest statistician and consistently account
for typos in both orthodox and unorthodox code; even then, any imputed
evidence against the unorthodox should account for the natural tendency
to make more errors with the unfamiliar than with the familiar, as well
as the impossibility of testing code (something we are constantly
advised to do, both explicitly and implicitly) that uses features not
currently available in the language.

This new excuse for a typo in well-regarded, orthodox examples seems to
rest upon a similarly weak foundation. At least I cannot ever recall
seeing anyone who had used the above attack retract it when a dozen of
his comrades turned out to have pointed out the same flaw at about the
same time, but surely that is no less proof of the readability of a
usage that one dislikes than it is on one's favorite tricks, eh? :-/

Ugh! That's far more words than this deserves, if not that the dubious
argument is being wielded by someone whose opinions deservedly carry a
lot of weight here.

--
People make secure systems insecure because
insecure systems do what people want and
secure systems don't. -- James Grimmelmann
Jul 18 '05 #16

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

Similar topics

1
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 =...
2
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...
1
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...
3
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...
3
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...
4
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....
11
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 =...
5
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.