overriding = operator

Anton Mellit mellit at gmail.com
Tue Apr 22 09:47:53 EDT 2008


Hi,

I am developing something like a compiler in Python, a library that
would help to generate machine-language code. One of the features is
the following. When I want to generate a piece of code I want  to
declare variables as follows:

x = var()
y = var()

This would generate no code, but it would mean that I need, say, two
32-bit integer variables. Then whenever I write something like x+y,
the '+' operator is overriden in such a way that a code which computes
the sum is generated. What I also want to do, I want to write
something like

z = var()
z = x + y

and I want a code which takes the sum of x and y and puts it in z to
be generated. However in python z = x + y already has its meaning and
it does something different from what I want. So I need something like
'overriding' =, which is impossible, but I look for a systematic
approach to do something instead. It seems there are two ways to do
what I need:

1. Implement a method 'assign' which generates the corresponding code
to store value:

z.assign(x + y)

2. Do the same as 1., but via property set methods. For example, this
would look cleaner:

z.value = x + y

Which of these is preferrable? Does anyone know any alternative ways?

Anton



More information about the Python-list mailing list