469,343 Members | 5,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

assignments - want a PEP

>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

Jul 18 '05 #1
3 1299
"Kay Schluehr" <ka**********@gmx.net> writes:
a = Symbol()
a

a


Use

a = Symbol('a')

instead and it should solve most of the problems you mention. What's
supposed to happen anyway, in your proposal, after

a = Symbol()
b = a
print b

?
Jul 18 '05 #2
On 31 Mar 2005 14:48:00 -0800, "Kay Schluehr" <ka**********@gmx.net> wrote:
From time to time I still use my old Mathematica system. TheMathematica 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:
aa

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

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.

If you let your symbols like a live in a CAS class instance attribute namespace,
you can do what you like, pretty much. I.e.,

cas = CAS()
cas.a # like your plain a, where you said "that's it"
cas.a, cas.b = cas.Expr(), cas.Expr()
(cas.a + cas.b)/cas.c

Etc.

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?

I think Guido does not want bare names to work like properties, even though
some of that could be interesting ;-)

Regards,
Bengt Richter
Jul 18 '05 #3

Bengt Richter wrote:
you can do what you like, pretty much. I.e.,

cas = CAS()
cas.a # like your plain a, where you said "that's it"
cas.a, cas.b = cas.Expr(), cas.Expr()
(cas.a + cas.b)/cas.c

Etc.


Hmmm... feels like a good idea if extended to use properties as special
constructors:
expr = CAS(Expr)
expr.a a type(expr.a) <type 'Expr'>
expr.a+expr.b a+b

or a bit shorter if Expr becomes the "master algebra":
_ = CAS(Expr)
_.a + _.b

a+b

Regarding that an object oriented CAS will mix an arbitray number of
algebras/namespaces that use algebraic operators homogenously
referencing namespaces is a reasonable for providing clarity.

A helpfull suggestion. Thanks Bengt!

Regards,
Kay
Regards,
Kay

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jacek Generowicz | last post: by
3 posts views Thread by bearophileHUGS | last post: by
21 posts views Thread by Paul Steckler | last post: by
8 posts views Thread by Fredrik Tolf | last post: by
reply views Thread by Simon Burton | last post: by
6 posts views Thread by Erik | last post: by
17 posts views Thread by Brian Blais | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.