overriding = operator

A

Anton Mellit

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
 
P

Paul McGuire

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

If you are willing to accept '<<=' as meaning 'assign to existing'
instead of 'left shift in place', you can override this operator using
the __ilshift__ method.

We used this technique in C++/CORBA code back in the 90's to "inject"
values into CORBA::Any variables.

-- Paul
 
A

Anton Mellit

If you are willing to accept '<<=' as meaning 'assign to existing'
instead of 'left shift in place', you can override this operator using
the __ilshift__ method.

We used this technique in C++/CORBA code back in the 90's to "inject"
values into CORBA::Any variables.

-- Paul

Great idea! It conflicts with 'left shift in place', which i also
need, but '=' seems more important.

Anton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top