Proto-PEP:可重载布尔运算符 [英] Proto-PEP: Overloadable Boolean Operators

查看:71
本文介绍了Proto-PEP:可重载布尔运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

邀请讨论以下proto-PEP。


------------------------- ------------------------------------


PEP? ?? - 可重载的布尔运算符

====================================== ==


摘要


这个PEP提出了一个扩展,允许对象定义自己的

含义布尔运算符''和'',''或''和''不'',并建议

是一种有效的实施策略。这个

实现的原型可从以下网址下载:

http://www.cosc.canterbury.ac.nz/~gr...hon_OBO.tar.gz

背景


Python目前不提供任何''__xxx__''特殊方法

对应''和'',' '或''和'''''布尔运算符。在''和''以及''或''的

情况下,最可能的原因是这些运算符

有短路语义,即第二个操作数如果可以从第一个操作数确定结果,则不评估


通常为这些操作员提供特殊方法的技术

因此不起作用。


没有这样的困难然而,不是的情况,并且

可以直接为这个

运算符提供特殊方法。因此,本提案的其余部分将集中于

,提供超载''和''以及''或''的方法。


动机

有很多应用程序可以自然地为Python运算符提供自定义的

含义,其中一些是布尔值

从那些能够定制的运营商中排除的运营商可能不方便。示例包括:


1. Numeric / Numarray,其中几乎所有运算符都定义在
数组上,以便在

对应的元素,并返回一个结果数组。对于

一致性,人们会期望两个数组之间的布尔运算

返回一个布尔数组,但目前这不可能。


此类扩展有一个先例:比较

运算符最初仅限于返回布尔结果,并且添加了
丰富的比较以便进行比较数字数组

可以返回布尔数组。


2.一个符号代数系统,其中一个Python表达式是

被评估在一个环境中导致它构造一个对应于表达式结构的
对象的树。


3.一个关系数据库接口,其中一个Python表达式是用于构造SQL查询的


经常建议的解决方法是使用按位运算符''&'',''| ''

和''〜''代替''和'',''或'''和'不,但是这有一定的

缺点。这些优先级与

其他运营商的优先级不同,并且它们可能已经用于其他目的(例如,示例1中的
)。还有美学上的考虑因素迫使用户使用除了最明显的语法之外的其他东西,而不是他们试图表达的内容。考虑到布尔运算是

SQL查询的主要内容,这在示例3的

情况下会特别严重。


要求


成功解决允许

布尔运算符定制问题的要求是:


1.在默认情况下(没有自定义),必须保留现有的

短路语义。


2.必须在默认情况下没有任何明显的速度损失

情况。


3.如果可能,自定义机制应该允许对象

提供短路或非短路语义,

自行决定。


一个明显的策略,已经提出过,将

传递给特殊方法第一个参数和

的函数来评估第二个参数。这将满足要求1和

3,但不满足要求2,因为它会产生构造函数对象的

的开销,并且可能会在

每个布尔运算。因此,这里不会进一步考虑




以下部分提出了一个实现,解决了所有

三个要求。这个实现的原型,以Python 2.3的形式提供补丁,可以从以下网址获得:

http://www.cosc.canterbury.ac.nz/~gr...hon_OBO.tar .gz

提案


特殊方法


在Python级别,对象可以定义以下特殊方法。


一元:__不__(自我)


二进制,第1阶段:__ and1 __(自我)__ or1 __(自我)

二进制,第2阶段:__ and2 __(自我,其他)__ or2 __(自我,其他)

__rand2 __(自我,其他)__ error2 __(自我,其他)


__not__方法,如果已定义,则实现''not''运算符。如果它没有定义
,或者它返回NotImplemented,则使用现有的语义




为了允许短路,处理''和''和''或''运营商

分为两个阶段。第一阶段在评估第一个

操作数之后但在第二个之前发生。如果第一个操作数定义

适当的阶段1方法,则使用第一个操作数调用它作为

参数。如果该方法可以在不需要

第二个操作数的情况下确定结果,则返回结果,并进一步处理

跳过。


如果阶段1方法确定需要第二个操作数,则它将返回特殊值NeedOtherOperand。这会触发第二个操作数的

评估,以及调用适当的

阶段2方法。


处理回退现有语义,如果在任何阶段找不到相关的

特殊方法或返回NotImplemented。


作为一种特殊情况,如果第一个操作数定义了一个阶段2方法但是

没有相应的阶段1方法,第二个操作数总是被评估并且第2阶段方法被调用。这允许一个不需要短路语义的对象简单地实现

相关的第2阶段方法并忽略第1阶段。

字节码


补丁添加了四个新的字节码,LOGICAL_AND_1,LOGICAL_AND_2,

LOGICAL_OR_1和LOGICAL_OR_2。作为它们的使用示例,为''和''表达式生成的

字节码如下所示:

Discussion is invited on the following proto-PEP.

-------------------------------------------------------------

PEP ??? - Overloadable Boolean Operators
========================================

SUMMARY

This PEP proposes an extension to permit objects to define their own
meanings for the boolean operators ''and'', ''or'' and ''not'', and suggests
an efficient strategy for implementation. A prototype of this
implementation is available for download from:

http://www.cosc.canterbury.ac.nz/~gr...hon_OBO.tar.gz
BACKGROUND

Python does not currently provide any ''__xxx__'' special methods
corresponding to the ''and'', ''or'' and ''not'' boolean operators. In the
case of ''and'' and ''or'', the most likely reason is that these operators
have short-circuiting semantics, i.e. the second operand is not
evaluated if the result can be determined from the first operand. The
usual techique of providing special methods for these operators
therefore would not work.

There is no such difficulty in the case of ''not'', however, and it
would be straightforward to provide a special method for this
operator. The rest of this proposal will therefore concentrate on
providing a way to overload ''and'' and ''or''.

MOTIVATION

There are many applications in which it is natural to provide custom
meanings for Python operators, and in some of these, having boolean
operators excluded from those able to be customised can be
inconvenient. Examples include:

1. Numeric/Numarray, in which almost all the operators are defined on
arrays so as to perform the appropriate operation between
corresponding elements, and return an array of the results. For
consistency, one would expect a boolean operation between two arrays
to return an array of booleans, but this is not currently possible.

There is a precedent for an extension of this kind: comparison
operators were originally restricted to returning boolean results, and
rich comparisons were added so that comparisons of Numeric arrays
could return arrays of booleans.

2. A symbolic algebra system, in which a Python expression is
evaluated in an environment which results in it constructing a tree of
objects corresponding to the structure of the expression.

3. A relational database interface, in which a Python expression is
used to construct an SQL query.

A workaround often suggested is to use the bitwise operators ''&'', ''|''
and ''~'' in place of ''and'', ''or'' and ''not'', but this has some
drawbacks. The precedence of these is different in relation to the
other operators, and they may already be in use for other purposes (as
in example 1). There is also the aesthetic consideration of forcing
users to use something other than the most obvious syntax for what
they are trying to express. This would be particularly acute in the
case of example 3, considering that boolean operations are a staple of
SQL queries.

REQUIREMENTS

The requirements for a successful solution to the problem of allowing
boolean operators to be customised are:

1. In the default case (where there is no customisation), the existing
short-circuiting semantics must be preserved.

2. There must not be any appreciable loss of speed in the default
case.

3. If possible, the customisation mechanism should allow the object to
provide either short-circuiting or non-short-circuiting semantics, at
its discretion.

One obvious strategy, that has been previously suggested, is to pass
into the special method the first argument and a function for
evaluating the second argument. This would satisfy requirements 1 and
3, but not requirement 2, since it would incur the overhead of
constructing a function object and possibly a Python function call on
every boolean operation. Therefore, it will not be considered further
here.

The following section proposes an implementation that addresses all
three requirements. A prototype of this implementation, in the form of
a patch to Python 2.3, is available from:

http://www.cosc.canterbury.ac.nz/~gr...hon_OBO.tar.gz
PROPOSAL

Special Methods

At the Python level, objects may define the following special methods.

Unary: __not__(self)

Binary, phase 1: __and1__(self) __or1__(self)

Binary, phase 2: __and2__(self, other) __or2__(self, other)
__rand2__(self, other) __ror2__(self, other)

The __not__ method, if defined, implements the ''not'' operator. If it
is not defined, or it returns NotImplemented, existing semantics are
used.

To permit short-circuiting, processing of the ''and'' and ''or'' operators
is split into two phases. Phase 1 occurs after evaluation of the first
operand but before the second. If the first operand defines the
appropriate phase 1 method, it is called with the first operand as
argument. If that method can determine the result without needing the
second operand, it returns the result, and further processing is
skipped.

If the phase 1 method determines that the second operand is needed, it
returns the special value NeedOtherOperand. This triggers the
evaluation of the second operand, and the calling of an appropriate
phase 2 method.

Processing falls back to existing semantics if at any stage a relevant
special method is not found or returns NotImplemented.

As a special case, if the first operand defines a phase 2 method but
no corresponding phase 1 method, the second operand is always
evaluated and the phase 2 method called. This allows an object which
does not want short-circuiting semantics to simply implement the
relevant phase 2 methods and ignore phase 1.
Bytecodes

The patch adds four new bytecodes, LOGICAL_AND_1, LOGICAL_AND_2,
LOGICAL_OR_1 and LOGICAL_OR_2. As an example of their use, the
bytecode generated for an ''and'' expression looks like this:

推荐答案

On Sun,05 Sep 2004 22:48:25 +1200,greg< gr ** @ cosc.canterbury.ac.nz>写道:
On Sun, 05 Sep 2004 22:48:25 +1200, greg <gr**@cosc.canterbury.ac.nz> wrote:
以下proto-PEP邀请讨论。

---------------------- ---------------------------------------

PEP ?? ? - 可重载的布尔运算符
========================================
Discussion is invited on the following proto-PEP.

-------------------------------------------------------------

PEP ??? - Overloadable Boolean Operators
========================================




如果我理解正确,那么对以下类的实例进行逻辑运算

应该复制
$ b的现有行为$ b布尔类型 - 这是正确的吗?


class MyBoolean:

def __init __(self,value):

self ._value = value


def __not __(self):

返回MyBoolean(不是self._value)


def __and1 __(self):

if(self._value):

返回NeedOtherOperand

else:

返回自我


def __and2 __(自我,其他):

返回自我

def __or1 __(自我):

if(self._value):

返回自我

否则:

返回NeedOtherOperand


def __or2 __(自我,其他):

返回自我

PEP没有明确描述__rand2 __(或__ror2__)何时

使用。当A定义

既没有__and1__也没有__and2__时,我猜它会被调用(A和B) - 这是正确的吗?


无论如何,我将不得不仔细看看你的补丁 - 我已经

玩弄了实现[或试图实施]的想法

我自己。



If I understand this correctly, then logical operations on instances
of the following class should duplicate the existing behaviour for the
boolean type -- is this correct?

class MyBoolean:
def __init__(self, value):
self._value = value

def __not__(self):
return MyBoolean(not self._value)

def __and1__(self):
if(self._value):
return NeedOtherOperand
else:
return self

def __and2__(self, other):
return self

def __or1__(self):
if(self._value):
return self
else:
return NeedOtherOperand

def __or2__(self, other):
return self
The PEP doesn''t explicitly describe when __rand2__ (or __ror2__) is
used. I''d guess that it will be called for (A and B) when A defines
neither __and1__ nor __and2__ -- is this correct?

Anyway, I''ll have to have a close look at your patch -- I''d been
toying with the idea of implementing [or attempting to implement] this
myself.


Andrew Durdin写道:
Andrew Durdin wrote:
如果我理解正确的话,那么以下类的实例的逻辑运算应该重复
布尔类型的现有行为 - 这是正确的吗?

def __and2 __(自我,其他):
返回自我


那应该是


返回其他


和__or2__类似。否则它看起来是正确的。

PEP没有明确描述何时使用__rand2 __(或__ror2__)。当A定义
既没有__and1__也没有__and2__时,我猜它会被调用(A和B) - 这是正确的吗?
If I understand this correctly, then logical operations on instances
of the following class should duplicate the existing behaviour for the
boolean type -- is this correct?

def __and2__(self, other):
return self
That should be

return other

and similarly for __or2__. Otherwise it looks right.
The PEP doesn''t explicitly describe when __rand2__ (or __ror2__) is
used. I''d guess that it will be called for (A and B) when A defines
neither __and1__ nor __and2__ -- is this correct?




Not究竟。每当需要第二个操作数

时,它就会被调用,第一个操作数没有定义__and2__,而第二个操作数确定__rand2__




完全陈述的确切规则相当复杂。我ñ
有一些文字说明了这一切 - 麻木细节,

但是我把它从PEP的初稿中删除了,因为害怕

在我有了这个想法之前吓跑了人们。也许

我会把它作为附录或其他东西包括在内。


Greg



Not exactly. It will get called whenever the second operand
is needed, the first operand doesn''t define __and2__, and the
second operand does define __rand2__.

The exact rules are rather complicated to state completely. I
have some text which spells it all out in mind-numbing detail,
but I left it out of the first draft of the PEP for fear of
scaring people off before I''d got the idea across. Perhaps
I''ll include it as an appendix or something.

Greg


你好greg,
Hello greg,
PEP ??? - 可重载的布尔运算符
======================================== />< snip>
一元:__不__(自我)

二进制,第1阶段:__ and1 __(自我)__或1 __(自我)

二进制,第2阶段: __and2 __(self,other)__ or2 __(self,other)
__ name2 __(self,other)__ error2 __(self,other)

< snip>
PEP ??? - Overloadable Boolean Operators
========================================
<snip>
Unary: __not__(self)

Binary, phase 1: __and1__(self) __or1__(self)

Binary, phase 2: __and2__(self, other) __or2__(self, other)
__rand2__(self, other) __ror2__(self, other)

<snip>



为什么不只是__bool __(自我) - >是的,错误的?

IMO这将满足您的所有需求,并且需要更少的更改。


再见。

-

------------------------------------------ ------------------------------

Miki Tebeka< mi ******* **@zoran.com>
http://tebeka.spymac.net

儿童和成人之间的唯一区别是玩具的价格


Why not just __bool__(self) -> True, False?
IMO this will answer all of your needs and will require less changes.

Bye.
--
------------------------------------------------------------------------
Miki Tebeka <mi*********@zoran.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys


这篇关于Proto-PEP:可重载布尔运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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