473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

overriding = operator

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
Jun 27 '08 #1
2 1551
On Apr 22, 8:47*am, "Anton Mellit" <mel...@gmail.comwrote:
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
Jun 27 '08 #2
On Apr 22, 3:54 pm, Paul McGuire <pt...@austin.rr.comwrote:
On Apr 22, 8:47 am, "Anton Mellit" <mel...@gmail.comwrote:
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
Great idea! It conflicts with 'left shift in place', which i also
need, but '=' seems more important.

Anton
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for...
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
11
by: Michael | last post by:
Hi, As an exercise, I created a class C that duplicates a value x thus having x_val1 and x_val2 I also overrided the + operator so that if I have z = x + y (x,y and z belong class C) then + ...
6
by: shoosh | last post by:
hi for my application under VC6 I need to implement my own memory manager which overrides the global new and delete operators and which Do Not use the normal free() and malloc(). it seemed to...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
8
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
2
by: linq936 | last post by:
Hi, I have this piece code, struct TriStr { MyString str1; MyString str2; MyString str3; TriStr(MyString s1, MyString s2, MyString s3){ this->str1 = s1;
1
by: SarahT | last post by:
Hi folks, I am doing something Very Bad and Wrong (which I'll spare you the details of) that requires overloading new for some specific classes. So, for example: class MyWeirdThingy {...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.