Operators for everything (was Re: Operators for matrix)

Paul Jackson pj at sgi.com
Sat Jul 29 15:00:12 EDT 2000


|> from matpy import elementwiseadd
|> ea=elementwiseadd
|> j = a @ea b

Pushing Paul Prescod's preference for "using alphanumerics
for new operators" a step further, how about a way to spell
the directive that "hereinafter in this parsing stream, this
name shall be parsed as a binary infix operator".

Say:
	__builtins__.syntax.binaryInfixOperator(f)

Then one could write:

    In module "matrixops":
	def elementwiseadd(a,b):
	    # elementwise matrix add of a, b
	syntax.binaryInfixOperator(elementwiseadd)

    In "myapplication":
	import matrixops
	j = a matrixops.elementwiseadd b

(See below for possible abbreviations.)

<sick> Added benefit: when the Forth guys discover Python,
they could lobby for:

	__builtins__.syntax.postfixOperator(f)

so that they could write

	a b c f		# instead of f(a,b,c)
</sick>

I will grant that this idea has problems:

1) a possible challenge for the parser,

2) and what's likely worse, a challenge for
   the language - whatever is the meaning (*)
   of attaching attributes to the name, rather
   than the thing named, and whatever are the
   compile time implications of such attributes?

   For example, if "myapplication" above had
   been written instead as:

	import matrixops
	ea = matrixops.elementwiseadd
	j = a ea b

   or as:

	from matrixops import elementwiseadd
	ea = elementwiseadd
	j = a ea b

   would this work - would the name "ea" acquire
   the attribute of being a binaryInfixOperator?

   (*) Perhaps we should ask L. Wall this.

3) Presumably, if a binary infix op appears without a
   preceding term for its left operand, then it is a
   reference to the function itself, rather than an
   invocation of the function (just as when a classic
   function name appears without a subsequent list of
   operands).

The line noise char '@' has the possible advantage
of warning the reader that unusual syntax is present.

But '@' has much the same problems for the language.
For instance, does the following 'work':

	from matpy import elementwiseadd
	ea = @elementwiseadd
	j = a ea b

Summary:
    In short, '@' is just a terse way of saying, each
    time you use it, that an operator is binary infix.

    I'd rather spell that out once, more verbosely.
-- 
-- 
I won't rest till it's the best ...	   Software Production Engineer
Paul Jackson (pj at sgi.com; pj at usa.net) 3x1373 http://sam.engr.sgi.com/pj



More information about the Python-list mailing list