PHP三元运算符等效于Python [英] the PHP ternary operator equivalent on Python

查看:73
本文介绍了PHP三元运算符等效于Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我想知道如何进行PHP三元运算符/声明

(...?...:。 ..)在Python中...


我想要的东西如下:


a = {''巨大的'':(数量> 90 )?真:假}


有什么建议吗?


谢谢


Daniel

解决方案

2005年11月18日10:53:04 -0800,Daniel Crespo< dc ***** @ gmail.com>写道:

我想知道如何在Python中执行PHP三元运算符/语句
(...?...:...)...



等待Python 2.5 - < http://www.python.org/peps/pep-0308.html>。


-

干杯,

Simon B,
si *** @ brunningonline.net
http://www.brunningonline .net / simon / blog /


2005年11月18日10:53:04 -0800,Daniel Crespo< dc ***** @ gmail .COM>写道:

我想知道如何在Python中执行PHP三元运算符/语句
(...?...:...)...



等待Python 2.5 - < http://www.python.org/peps/pep-0308.html>。


-

干杯,

Simon B,
si *** @ brunningonline.net
http://www.brunningonline .net / simon / blog /


Daniel Crespo写道:

我想知道如何做PHP三元组操作员/声明
(...?...:...)在Python中...

我想要的是:

a = { ''巨大'':(数量> 90)? True:False}




嗯,在你的例子中''>''运算符已经返回一个布尔值

所以你可以直接使用它。 Hoewver,我同意有一个三元运算符会很好的情况。

情况。不幸的是,

Python并不直接支持这个;最接近我发现的
是:

(value_if_false,value_if_true )[boolean_value]


这利用了False和True转换为整数的事实,分别为
0和1。缺点是它真的很难看。

另外,它没有使用最低限度的评估;换句话说,如果你尝试

表达式,如:

(无,func())[callable(func)]


你可能会认为这会返回func()的值,如果它是可调用的,

,否则为None。不幸的是,func()无论如何都会被评估,即使条件是假的,也需要



当然,你可以通过真正做到这一点来解决这个问题神秘的东西

with lambdas:

(lambda:None,lambda:func())[callable(func)]()




....但到那时,你最好只使用if / then / else。


- 大卫


Hi!

I would like to know how can I do the PHP ternary operator/statement
(... ? ... : ...) in Python...

I want to something like:

a = {''Huge'': (quantity>90) ? True : False}

Any suggestions?

Thanks

Daniel

解决方案

On 18 Nov 2005 10:53:04 -0800, Daniel Crespo <dc*****@gmail.com> wrote:

I would like to know how can I do the PHP ternary operator/statement
(... ? ... : ...) in Python...



Wait for Python 2.5 - <http://www.python.org/peps/pep-0308.html>.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/


On 18 Nov 2005 10:53:04 -0800, Daniel Crespo <dc*****@gmail.com> wrote:

I would like to know how can I do the PHP ternary operator/statement
(... ? ... : ...) in Python...



Wait for Python 2.5 - <http://www.python.org/peps/pep-0308.html>.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/


Daniel Crespo wrote:

I would like to know how can I do the PHP ternary operator/statement
(... ? ... : ...) in Python...

I want to something like:

a = {''Huge'': (quantity>90) ? True : False}



Well, in your example the ''>'' operator already returns a boolean value
so you can just use it directly. Hoewver, I agree that there are
situations in which a ternary operator would be nice. Unfortunately,
Python doesn''t support this directly; the closest approximation I''ve
found is:

(value_if_false, value_if_true)[boolean_value]
This exploits the fact that False and True are converted to integers as
zero and one, respectively. The downside is that it''s really ugly.
Also, it doesn''t use minimal evaluation; in other words, if you try an
expression like:
(None, func())[callable(func)]
You might think this would return the value of func() if it''s callable,
and None otherwise. Unfortunately, func() is evaluated no matter what,
even if the condition is false.

Of course, you can always get around this by doing really cryptic stuff
with lambdas:
(lambda: None, lambda: func())[callable(func)]()



.... but by that point, you''re better off just using an if/then/else.

-- David


这篇关于PHP三元运算符等效于Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆