user-defined operators: a very modest proposal

Steve R. Hastings steveha at localhost.localdomain
Tue Nov 22 19:08:41 EST 2005


> 	if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation'
> Since it contains ']+[' I assume it must now be parsed as a user-defined
> operator, but this code currently has a meaning in Python.

Yes.  I agree that this is a fatal flaw in my suggestion.

Perhaps there is no syntax that can be done inside the bounds of ASCII
that will please everyone and not break existing code.


Your suggestion of Unicode makes a lot of sense.  There are glyphs for
math operators, and if Python can accept Unicode source files, that seems
to me like a much better solution than hacks involving ASCII characters.

I didn't notice it before, but PEP 263 allows Python source files to be
Unicode:

http://www.python.org/peps/pep-0263.html

So the latest versions of Python already have support for Unicode source
files!


Could such Unicode sources be exported to ASCII for porting code to
platforms that don't allow Unicode Python files?  Yes: just replace the
Unicode character with a symbol like __op__, where op is the operator.

Actually, that's a better syntax than the one I proposed, too:

__+__
# __add__	# this one's already in use, so not allowed
__outer*__


-- 
Steve R. Hastings    "Vita est"
steve at hastings.org    http://www.blarg.net/~steveha




More information about the Python-list mailing list