[PEP draft 2] Adding new math operators

Huaiyu Zhu hzhu at localhost.localdomain
Tue Aug 8 21:42:58 EDT 2000


On 09 Aug 2000 01:01:21 +0200, Bernhard Herzog <herzog at online.de> wrote:
>hzhu at localhost.localdomain (Huaiyu Zhu) writes:
>
>> The symbol ~ is already used in Python as the unary "bitwise not" operator.
>> Currently it is not allowed for binary operators, so using it as a prefix to
>> binary operators will not create incompatibility.
>
>There are a couple of cases where this is incompatible. The expression
>~-1 is valid Python today and is parsed as the three tokens ~ - 1. With
>the proposed new operators, ~- will be recognized as one token so the
>expression will parse as the two tokens ~- 1 which is invalid because ~-
>is a binary operator. The other case is ~+1.

>From what I understand, the parser can distinguish unary and binary
operators, so that ~-1 will not be parsed as ~- 1, because it's not
following an operand.   

>>    - The bitwise operators assign special syntactical and semantical
>>      structures to operations that are not as fundamental as math.  
>
>That's a pretty bold statement to make about a general purpose
>programming language. One could easily argue that bitwise operation are
>much more fundamental than matrix operations on a computer. Even though
>Python is much higher level than e.g. Assembler or C, there are lots of
>uses for these operations, like e.g. reading and writing binary files or
>determining the status of mouse buttons in a GUI (in tkinter you have to
>check the individual bits of the state attribute of the event)

Well, conceptually, two sets of operators are equally fundamental: the
arithmetic type operators + - * / and the lattice type operators (and) (or)
(not) (xor), etc.  Both of them have two versions, objectwise and
elementwise. The bitwise operators are elementwise lattice operators when
each bit in an integer is regarded as an element.

Elementwise operators in the form of ~and ~or ~not ~xor is as fundamental as
the elementwise arithmetic operators in the form of ~+ ~- ~* ~/.  However,
the specific restriction of the former to bitwise operation on integers is
not as fundamental.

If we view Python as a high level OO lanauge, we should be more concerned
with the potential of operator overloading on arbitary objects than on the
particular builtin types.  The distinction between elementwise and
objectwise operations is not limited to numerical computation.

>Of course, this doesn't mean that Python really needs so many different
>symbols for this purpose. Named operators like bitand might be more
>Pythonic and ^ would be better suited for pow.

True.

-- 
Huaiyu Zhu                       hzhu at users.sourceforge.net
Matrix for Python Project        http://MatPy.sourceforge.net 



More information about the Python-list mailing list