473,320 Members | 1,909 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,320 software developers and data experts.

if in expression

Hi,
I'm using IronPython to evaluate expressions, so I can't use the
return statement but have to say it in an one-liner. Using C/C++ I
could use "cond ? then : else" to have an expression-if, but in Python
there's no such operator. The "cond and then or else"-trick only seems
to work for non-false "then"s and is not very readable.
So is the pythonic way to def iif(cond, then, else_): if cond: return
then; else: else_; ? But actually this is not the same, because "then"
and "else_" are evaluated independently of cond...

What is the right way to have if in an expression?

Thanks,
Christian
Aug 18 '08 #1
8 1664
ch****************@gmx.de schrieb:
Hi,
I'm using IronPython to evaluate expressions, so I can't use the
return statement but have to say it in an one-liner. Using C/C++ I
could use "cond ? then : else" to have an expression-if, but in Python
there's no such operator. The "cond and then or else"-trick only seems
to work for non-false "then"s and is not very readable.
So is the pythonic way to def iif(cond, then, else_): if cond: return
then; else: else_; ? But actually this is not the same, because "then"
and "else_" are evaluated independently of cond...

What is the right way to have if in an expression?
Since python 2.5, it is

<then_valueif <condelse <else_value>

If you want lazy evaluation, you can use lambdas:

iif(cond, lambda: then, lambda: else_)()

Diez
Aug 18 '08 #2
On Aug 18, 5:46*pm, christian2.schm...@gmx.de wrote:
Hi,
I'm using IronPython to evaluate expressions, so I can't use the
return statement but have to say it in an one-liner.
By "evaluate expressions", do you mean using the eval built-in
function? If so, find the recent thread addressing this topic; it was
asserted that 98% of eval() use-cases were better implemented another
way. What's _your_ use-case?
Using C/C++ I
could use "cond ? then : else" to have an expression-if, but in Python
there's no such operator. The "cond and then or else"-trick only seems
to work for non-false "then"s and is not very readable.
There is an even more unreadable hack (due to Tim Peters IIRC) that
avoides the false-then problem:

(cond and [then_value] or [else_value])[0]

Aug 18 '08 #3
"Diez B. Roggisch" <de***@nospam.web.dewrites:
Since python 2.5, it is

<then_valueif <condelse <else_value>

If you want lazy evaluation, you can use lambdas:

iif(cond, lambda: then, lambda: else_)()
Your code uses "iif" and attempts to evaluate a tuple; could you post
an example that works?

I ask because it's not clear what you mean by lazy evaluation in this
context. The ternary "if" expression introduced in Python 2.5 only
evaluates then_value or else_value depending on the outcome of cond.
How is that different than using a lambda?
Aug 18 '08 #4
John Machin wrote:
There is an even more unreadable hack (due to Tim Peters IIRC) that
avoides the false-then problem:

(cond and [then_value] or [else_value])[0]
The correct attribution is "due to Tim Peters (who wishes it was Steve
Majewski)."

</F>

Aug 18 '08 #5
Hrvoje Niksic schrieb:
"Diez B. Roggisch" <de***@nospam.web.dewrites:
>Since python 2.5, it is

<then_valueif <condelse <else_value>

If you want lazy evaluation, you can use lambdas:

iif(cond, lambda: then, lambda: else_)()

Your code uses "iif" and attempts to evaluate a tuple; could you post
an example that works?
I ask because it's not clear what you mean by lazy evaluation in this
context. The ternary "if" expression introduced in Python 2.5 only
evaluates then_value or else_value depending on the outcome of cond.
How is that different than using a lambda?

If iif is defined as this:

def iif(cond, then, else_):
if cond:
return then
else:
return else_

you have the problem that "then" and "else_" get evaluated *before* they
get passed. So for example this factorial function will fail with too
deep recursion error:

def f(n):
return iif(n1, n * f(n-1), 1)

But if you do it like this, the then and else_ will be functions that
get returned and then they need to be called to be acutally evaluated:

def f(n):
return iif(n1, lambda: n * f(n-1), lambda: 1)()
Diez
Aug 18 '08 #6
Hrvoje Niksic wrote:
>If you want lazy evaluation, you can use lambdas:

iif(cond, lambda: then, lambda: else_)()

Your code uses "iif" and attempts to evaluate a tuple; could you post
an example that works?

I ask because it's not clear what you mean by lazy evaluation in this
context. The ternary "if" expression introduced in Python 2.5 only
evaluates then_value or else_value depending on the outcome of cond.
How is that different than using a lambda?
the second part of Diez' answer is a reply to the second part of the
OP's question.

</F>

Aug 18 '08 #7
Fredrik Lundh <fr*****@pythonware.comwrites:
Hrvoje Niksic wrote:
>>If you want lazy evaluation, you can use lambdas:

iif(cond, lambda: then, lambda: else_)()

Your code uses "iif" and attempts to evaluate a tuple; could you post
an example that works?

I ask because it's not clear what you mean by lazy evaluation in this
context. The ternary "if" expression introduced in Python 2.5 only
evaluates then_value or else_value depending on the outcome of cond.
How is that different than using a lambda?

the second part of Diez' answer is a reply to the second part of the
OP's question.
Ah, that explains it. Sorry about the noise!
Aug 18 '08 #8
On 18 Aug., 10:22, John Machin <sjmac...@lexicon.netwrote:
On Aug 18, 5:46 pm, christian2.schm...@gmx.de wrote:
I'm using IronPython to evaluate expressions, so I can't use the
return statement but have to say it in an one-liner.

By "evaluate expressions", do you mean using the eval built-in
function?Ifso, find the recent thread addressing this topic; it was
asserted that 98% of eval() use-cases were better implemented another
way. What's _your_ use-case?
User provided python expression of .net's CLR-objects (with
overloaded
operators). The result is processed again in .net.
Reading through the mentioned mail, I think I'm in the 2%. :-)

Thanks,
Christian
Aug 18 '08 #9

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

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
22
by: Tony Johansson | last post by:
Hello Experts! I'm reading i a book about C++ and they mention infix with telling what it is. I hope you out there can do so. Many thanks! //Tony
2
by: Mike Turco | last post by:
I like using the expression builder for a lot of different things but it isn't always available when I want to use it, for example in the code window, or in all of the control properties. I am...
14
by: John Temples | last post by:
Given this code: extern volatile unsigned char v; int main(void) { v; return 0; }
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
1
by: lovecreatesbea... | last post by:
---quoting--- Annex C (informative) Sequence points 1 The following are the sequence points described in 5.1.2.3: - The end of a full expression: an initializer (6.7.8); the expression in an...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
18
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.