Arbitrary operator symbols (Re: Operators for matrix: current choices)

Greg Ewing see at my.signature
Thu Jul 20 01:04:15 EDT 2000


The main problem I see with any kind of scheme requiring
new operators to be defined before use is how you control
their scope.

Presumably they should be limited in scope to the module
where they are defined or imported, so that different
modules can define their own sets of operators without
conflicting with each other.

That means there has to be some way of importing
operators. And whatever definition and importing scheme
is used has to operate at compile-time, not run-time,
so the proposals made so far which involve executing
Python code to define operators aren't going to work.
Some sort of special syntax is needed.

Further, how do you manage a conflict between two modules
that you want to use at the same time, but they define
conflicting sets of operators? With names, you always
have the option of using qualified names, but that
doesn't work with operators.

I think the only way to make something like this work is
if there is no need to declare operators at all -- you
just use them. This means there have to be fixed ways
of (1) deciding the precedence of an arbitrary operator
and (2) mapping it to a method name.

(1) can be done by, e.g. requiring all operators to begin
or end with one of the standard operators, from which it
takes its precedence.

(2) can be done by extending the function definition syntax
to allow the name actual operator string to be used as
the name of the method, so no mapping is required. e.g.

  class Matrix:
    def ".*"(self, other):
      # do an elementwise mul here

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list