assignments - want a PEP

Kay Schluehr kay.schluehr at gmx.net
Thu Mar 31 17:48:00 EST 2005


>From time to time I still use my old Mathematica system. The
Mathematica language has some nice properties. The one I like best is
the possibility to create symbols from nothing. Translated into the
Python realm following creations are possible:

>>> a
a

That's it. Just make an 'a' as a pure symbol. The reason for those
symbols is late evaluation. Most of the time an expression should be
transformed and simplified before it is finally evaluated using
concrete numbers or other stuff. I tried to mimic this in Python using
a Symbol() class:

>>> a = Symbol()
>>> a
a

So 'a' would be a Symbol object that is represented by the name "a" not
by the standard format information that tells us that a is some member
of class Symbol(). Starting with Symbol() subclsses of Symbol() e.g
Expr() could overload operators like __add__, __mul__ etc. in order to
create new symbolic objects:

>>> a,b = Expr(),Expr()
>>> (a+b)/c
a+b
---
 c

The availability of Symbol() would make it easy to create a Computer
Algebra System ( CAS ) in Python, just using standard Python operators.

Unfortunately it is not possible to implement Symbol() in an elegant
way. Symbol() has to retrieve somehow it's own name from the
environment where the name is created, but no assignment/name binding
event is accessible to Python objects.

I want to propose making name binding accessible by introducing an
__assign__ method which is called, whenever an object is bound to a
name. By default the __assign__ method does nothing at all but can be
implemented by custom classes.

def __assign__(self,own_name):
    # do somtehing with me and my name

Instead of changing the behaviour of the current assignment operator
"=" one could think about introduction of a new assignment ":=" that is
connected to the __assign__ method of custom classes if available.

What do You think?

Regards,
Kay




More information about the Python-list mailing list