PEP-能?表达条件 [英] PEP-able? Expressional conditions

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

问题描述

Pythons匿名函数lambda的一个主要原因被认为是bb被破坏了。是Pythons将语句放入表达式中的残疾

并支持完整功能。许多尝试改进lambda语法

也试图以一种或另一种方式打破

中的表达/语句区别。这些建议都没有成功解决由蟒蛇缩进造成的严重问题

语法,我猜他们永远不会成功。另一方面,我不会立即看到必须支持全系列的Python

构造在小型匿名函数中,这些函数专用于

一行。


而不是将语句推送到表达式中,可以尝试以其他方式执行

,并使用

语句。最重要的用例是作业和

条件。我只想考虑条件。


在Python条件语句中有以下形式:


如果COND_1:

BLOCK_1

elif COND_2:

BLOCK_2

....

else:

BLOCK_n


在将这种语句转换成表达式之前,我们必须将
限制BLOCK为表达式:


如果COND_1:

EXPR_1

elif COND_2:

EXPR_2

....

else:

EXPR_n


由于条件语句是按顺序遍历的,我们可以将
转换为序列(COND,EXPR)对。最后我们必须恢复条件语义。


我想提出一个新的关联二元运算符,它作用于

(在COND评估为True的情况下,COND,EXPR)对像

对的第二个EXPR参数上的投影进行配对。


此函数非常类似于


def cond(pair1,pair2):

COND1,EXPR1 = pair1

如果COND1:

返回EXPR1

试试:

COND2,EXPR2 = pair2

如果COND2:

返回EXPR2

除TypeError:

返回pair2

替代语法建议:


(a)(COND1,EXPR1)| | (COND2,EXPR2)

(b)(COND1,EXPR1)案例(COND2,EXPR2)

(c)(COND1,EXPR1)owise(COND2,EXPR2)

(d)(COND1,EXPR1)? (COND2,EXPR2)


问候,



解决方案

2005年9月7日星期三上午05:29,Kay Schluehr写道:

而不是将语句推送到表达式中,可以尝试以其他方式执行此操作,并使用
语句的功能。
备选语法建议:

(a)(COND1,EXPR1)|| (COND2,EXPR2)
(b)(COND1,EXPR1)案例(COND2,EXPR2)
(c)(COND1,EXPR1)owise(COND2,EXPR2)
(d)(COND1 ,EXPR1)? (COND2,EXPR2)




您似乎正在重塑C三元运算符。这绝对是一匹死马。
绝对是一匹死马。已经有了一个PEP,它被拒绝了。


如果你真的想要这个,你将不得不实施它

带功能:


def ternary(condition,true_result,false_result):

条件:

返回true_result

否则:

返回false_result


几乎一样好,你不必把curmudgeons说成提供

给你。


干杯,

特里


- < br $>
Terry Hancock(hancock at anansispaceworks.com)

Anansi Spaceworks http://www.anansispaceworks.com


Terry Hancock写道:

9月7日星期三2005年上午05:29,Kay Schluehr写道:

而不是将语句推送到表达式中,可以尝试这样做
其他方式r具有
语句功能的声音和模型表达式。

替代语法建议:

(a)(COND1,EXPR1)|| (COND2,EXPR2)
(b)(COND1,EXPR1)案例(COND2,EXPR2)
(c)(COND1,EXPR1)owise(COND2,EXPR2)
(d)(COND1 ,EXPR1)? (COND2,EXPR2)



您似乎正在重塑C三元运算符。这绝对是一匹死马。已经有一个PEP了,它被拒绝了。




好​​吧,我没有受到C的启发而且运算符不是三元数而是二进制

和associative。然而,三元条件

运算符的行为作为极限情况存在。表达式变得更像是一种水平挤压开关。因此,情况是指情况。关键字提议。


如果使用更多

条款链接表达式,可能会更加明显:


( a'')(COND1,EXPR1)|| (COND2,EXPR2)|| ...... || (CONDk,EXPRk)

(b'')(COND1,EXPR1)案例(COND2,EXPR2)案例......案例(CONDk,EXPRk)

如果你真的想要这个,你将不得不用一个函数实现它

def ternary(condition,true_result,false_result):
如果条件:
返回true_result
否则:
返回false_result




不,正如我解释的那样,它不是一个三元运算符,它不容易

有效地使用Python函数实现,因为Python不支持延迟评估
。通常不想评估所有

条件以及所有结果(当将它们传递到

函数时)但是按顺序评估条件表达式并停止

在第一个真实条件下。好吧,我确实希望进一步推进

并以这种方式引入懒惰元组,但我想首先注意到

对明显用例的响应。


Kay




" Kay Schluehr" < KA ********** @ gmx.net>在消息中写道

news:11 ********************* @ g44g2000cwa.googlegro ups.com ...

不,正如我解释的那样,它不是一个三元运算符,并且不能有效地使用Python函数实现,因为Python不支持惰性求值。


通过*小心*使用流量控制操作符''和''和''或'',你可以

经常得到你想要的*现在*,不需要PEP。

通常不想评估所有
条件以及所有结果(将它们传递给
函数时)但是评估条件表达式顺序并在第一个真实条件下停止。




*如果* bool(result_expression_i)==对所有i都是真的,(可能是最后的

默认表达式),对于某些实际用例是正确的,然后

后面的表达式计算结果对应于第一个

''true' '条件(如果有的话)或默认值:


c0和r0或c1和r1或c2和r2 ...或默认值。


我只看到一对和一对的真实例子,如(x <0)和-x或x

绝对值。


Terry J. Reedy


One of the main reasons Pythons anonymous function lambda is considered
to be "broken" is Pythons disability to put statements into expressions
and support full functionality. Many attempts to improve lambdas syntax
had also been attempts to break the expression/statement distinction in
one or the other way. None of this suggestions have ever been
successfull in solving severe problems caused by Pythons indentation
syntax and I guess they will never succeed. On the other hand I do not
see immediately the necessaty to support the full range of Python
constructs in small anonymous functions that are dedicated to fit into
one line.

Instead of pushing statements into expressions one can try to do it the
other way round and model expressions with the functionality of
statements. The most important use-cases are assignments and
conditions. I want to consider conditions only.

In Python conditional statements have the form:

if COND_1:
BLOCK_1
elif COND_2:
BLOCK_2
....
else:
BLOCK_n

Before turning this kind of statement into an expression we have to
restrict the BLOCKs to expressions:

if COND_1:
EXPR_1
elif COND_2:
EXPR_2
....
else:
EXPR_n

Since the conditional statement is traversed sequentially we can
transform it into a sequence of (COND,EXPR) pairs. Finally we have to
recover the conditional semantics.

I want to propose a new associative binary operator that acts on
(COND,EXPR) pairs like a projection on the second EXPR argument of a
pair in case of COND evaluated True.

This function works much like

def cond(pair1, pair2):
COND1,EXPR1 = pair1
if COND1:
return EXPR1
try:
COND2,EXPR2 = pair2
if COND2:
return EXPR2
except TypeError:
return pair2
Alternative syntax proposals:

(a) (COND1,EXPR1) || (COND2,EXPR2)
(b) (COND1,EXPR1) case (COND2,EXPR2)
(c) (COND1,EXPR1) owise (COND2,EXPR2)
(d) (COND1,EXPR1) ? (COND2,EXPR2)

Regards,
Kay

解决方案

On Wednesday 07 September 2005 05:29 am, Kay Schluehr wrote:

Instead of pushing statements into expressions one can try to do it the
other way round and model expressions with the functionality of
statements. Alternative syntax proposals:

(a) (COND1,EXPR1) || (COND2,EXPR2)
(b) (COND1,EXPR1) case (COND2,EXPR2)
(c) (COND1,EXPR1) owise (COND2,EXPR2)
(d) (COND1,EXPR1) ? (COND2,EXPR2)



You appear to be reinventing the C "ternary operator". This is
definitely a dead horse. There was already a PEP, and it was
refused.

If you actually want this, you''re going to have to implement it
with a function:

def ternary(condition, true_result, false_result):
if condition:
return true_result
else:
return false_result

Almost as good, and you don''t have to talk curmudgeons into providing
it for you.

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com


Terry Hancock wrote:

On Wednesday 07 September 2005 05:29 am, Kay Schluehr wrote:

Instead of pushing statements into expressions one can try to do it the
other way round and model expressions with the functionality of
statements.

Alternative syntax proposals:

(a) (COND1,EXPR1) || (COND2,EXPR2)
(b) (COND1,EXPR1) case (COND2,EXPR2)
(c) (COND1,EXPR1) owise (COND2,EXPR2)
(d) (COND1,EXPR1) ? (COND2,EXPR2)



You appear to be reinventing the C "ternary operator". This is
definitely a dead horse. There was already a PEP, and it was
refused.



Well, I''m not inspired by C and the operator is not ternary but binary
and associative. Nevertheless the behaviour of the ternary condition
operator exists as a limit case. The expression becomes more a kind of
a horizontal squeezed switch. Therefore the "case" keyword proposal.

It might become more obvious if one chains the expression using more
terms:

(a'') (COND1,EXPR1) || (COND2,EXPR2) || ... || (CONDk,EXPRk)
(b'') (COND1,EXPR1) case (COND2,EXPR2) case ... case (CONDk,EXPRk)
If you actually want this, you''re going to have to implement it
with a function:

def ternary(condition, true_result, false_result):
if condition:
return true_result
else:
return false_result



No, as I explained it is not a ternary operator and it can''t easily be
implemented using a Python function efficiently because Python does not
support lazy evaluation. One usually does not want to evaluate all
conditions as well as all the results ( when passing them into the
function ) but evaluate conditional expressions sequentially and stop
at the first true condition. Well I would indeed like to go even
further and introduce lazy tuples this way but I wanted to notice the
responses to an obvious use case first.

Kay



"Kay Schluehr" <ka**********@gmx.net> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...

No, as I explained it is not a ternary operator and it can''t easily be
implemented using a Python function efficiently because Python does not
support lazy evaluation.
By *carefully* using the flow-control operators ''and'' and ''or'', you can
often get what you want *now*, no PEP required.
One usually does not want to evaluate all
conditions as well as all the results ( when passing them into the
function ) but evaluate conditional expressions sequentially and stop
at the first true condition.



*If* bool(result_expression_i) == True for all i, (except maybe last
default expression), which is true for some actual use cases, then the
following expression evaluates to the result corresponding to the first
''true'' condition (if there is one) or to the default:

c0 and r0 or c1 and r1 or c2 and r2... or default.

I have only seen real examples with one and-pair, like (x < 0) and -x or x
for absolute value.

Terry J. Reedy


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

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