[PEP draft 2] Adding new math operators

Bernhard Herzog herzog at online.de
Tue Aug 8 19:01:21 EDT 2000


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.

Of course, this might not be much of a problem in practice. I haven't
found any instance of ~- in the Python code I currently have on my
harddrive.

You could probably work around it by also allowing ~+ and ~- to be used
as unary operators, but that seems awkward.


>    - 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)

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.


-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list