内联条件? [英] Inline Conditionals?

查看:108
本文介绍了内联条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有计划在Python中包含内联条件?例如:


def isNegative(x):

返回x< 0?真:错误


谢谢!


-jag


-

Joshua Ginsberg< jo *** @ brainstorminternet.net>

头脑风暴互联网网络运营

解决方案

" Joshua Ginsberg" <乔*** @ brainstorminternet.net>在留言中写道

新闻:ma ************************************ ** @ pyth on.org ...

有没有计划在Python中包含内联条件?例如:

def isNegative(x):
返回x< 0?真:错误

谢谢!

-jag

- Joshua Ginsberg< jo *** @ brainstorminternet.net>
头脑风暴互联网运营




如下:

< blockquote class =post_quotes> def iif(condition,true = True,false = False):
.... if condition:

.... return true

....返回false

.... iif(''foo''==''bar'',''w00t'',''l33t'')
''l33t''iif(''bar''==''bar'',''w00t'',''l33t'')
''w00t''iif''''bar' '==''bar'')
真iif(''foo''==''bar'')
False




希望这会有所帮助。


Adonis


Adonis写道:

怎么样:

def iif(condition,true = True,false = False):



。 ..如果条件:
...返回true
...返回false
...




只是一个想法,但是如果与

函数调用一起使用会产生意想不到的结果,如果这些函数调用有副作用,因为两者都是

评估,而三元运算符仅用于其他语言

评估其中一个可能的表达式。


Joshua Ginsberg< jo *** @ brainstorminternet.net>写道:

有没有计划在Python中包含内联条件?例如:

def isNegative(x):
返回x< 0? True:False




阅读PEP 308,并注意这可能是Python历史上最大的火焰战争(好吧,在静止之前)正在进行的装饰工作

讨论......)。


但是要回答你的问题:不。


干杯,

mwh

-

Linux:马。像野马一样,骑行很有趣。也很容易因为它没有像你的袜子一样砸到你身上而砸到地上。 - Jim的操作系统谱系,asr


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

解决方案

"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


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***@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


这篇关于内联条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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