如果短 [英] Short if

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

问题描述

你好


如果像C或PHP那样Python有短小的内容:


bool = false

string =''这是''+((bool)?''true'':''false'')





-

Mvh。

/ Thomas

Hello

Does Python have a short if like C or PHP:

bool = false
string = ''This is '' + (( bool ) ? ''true'' : ''false'')

?

--
Mvh.
/Thomas

推荐答案

* Thomas compga.py中的Lindgaard< th **** @ it-snedkeren.BLACK_HOLE.dk>

* Thomas Lindgaard <th****@it-snedkeren.BLACK_HOLE.dk>
in comp.lang.python:
如果像C或PHP那样Python有短吗:
Does Python have a short if like C or PHP:




< http://www.python.org/doc/faq/programming.html#is-there-an-equivalent-of-cs-ternary -operator>


-

DW



<http://www.python.org/doc/faq/programming.html#is-there-an-equivalent-of-c-s-ternary-operator>

--
DW


Damien Wyart写道:
Damien Wyart wrote:
* Thomas Lindgaard< th **** @ it-snedkeren.BLACK_HOLE.dk>
* Thomas Lindgaard <th****@it-snedkeren.BLACK_HOLE.dk>
in comp.lang.python:
Python是否有短的,如果像C或者PHP:
Does Python have a short if like C or PHP:



< http://www.python.org/doc/faq/programming.html#is -there-an-equivalent-of-c-s-ternary-operator>



<http://www.python.org/doc/faq/programming.html#is-there-an-equivalent-of-c-s-ternary-operator>




此功能的拒绝是最终的吗?我的意思是,关于

构造的讨论是否过时?


Reinhold


-

Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows

mitbr?chte,w?re das bedauerlich。 bei Windows der Umfang eines

" kompletten Betriebssystems" ist,nennt man bei Linux eine Rescuedisk。

- David Kastrup在de.comp.os.unix.linux.misc



Is the reject of this feature final? I mean, is discussion about the
construct obsolete?

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbr?chte, w?re das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc


Thomas Lindgaard写道:
Thomas Lindgaard wrote:
你好

如果像C或PHP那样Python有短小的东西:

bool = false
string =''这个是''+((bool)?''true'':''false'')

Hello

Does Python have a short if like C or PHP:

bool = false
string = ''This is '' + (( bool ) ? ''true'' : ''false'')

?




不。 PEP308引入了一个像成语这样的三元运算符已被拒绝(参见:
http://www.sourcekeg.co.uk/www.pytho...pep-0308.html)

但你可以模仿它:



Nope. PEP308 to introduce a ternary operator like idiom has been
rejected (see:
http://www.sourcekeg.co.uk/www.pytho...pep-0308.html),
but you can emulate it:

bool = False
s ="这是 +(''false'',''true'')[bool]
print s
bool = False
s = "This is " + (''false'', ''true'')[bool]
print s



这是假的


在这种情况下,bool计算结果为0,''false''表示元组的索引0

(''false'',''true'')。


-

Vincent Wehren


This is false

In this case bool evaluates to 0 and ''false'' is at index 0 of the tuple
(''false'', ''true'').

--
Vincent Wehren


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

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