473,395 Members | 2,436 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,395 software developers and data experts.

overriding '=' operator?


Is it possible to override assignment, the
way that '+' can be overridden for example?



Jul 18 '05 #1
4 14326
us**@domain.invalid wrote:
Is it possible to override assignment, the
way that '+' can be overridden for example?

You can in statements like

a[i] = something


a.x = something

by overriding methods on the a object, but you cannot override the
behavior for

a = something

Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ After a thousand years or so you go native.
\__/ Camden Benares
Jul 18 '05 #2

<us**@domain.invalid> wrote in message
news:5e******************************@news.teranew s.com...

Is it possible to override assignment, the
way that '+' can be overridden for example?
No, because "=" isn't an operator, it's a statement.

John Roth


Jul 18 '05 #3
On Thu, 30 Oct 2003 20:32:52 GMT, us**@domain.invalid wrote:

Is it possible to override assignment, the
way that '+' can be overridden for example?

No, not for the simple 'variable = value' case, because '=' doesn't
modify a value.

In Python, assignment binds the RHS value to the LHS name. The type of
the previous LHS value is irrelevant - in Python, the type is a
property of the value rather than the variable.

The object previously bound to that variable is unaffected (apart from
reference counting and possible garbage collection) and isn't really
involved in the assignment. And as it isn't involved, it makes no
sense for it to define what the assignment operator does.

In short, you should think of the assignment operator as assigning to
the variable - not to whatever object that variable happened to be
bound to before the assignment.

This is, of course, very different to statically typed languages like
Ada and C++ where it makes sense for the assignment operator to be
overridden based on the type - the type being associated with the
variable, in these cases, rather than the value that happens to be
stored there.

If you really want to override assignment, one way would be to define
a property. Instead of 'var = value' you would then write
'var.propertyname = value'. Using a property, assignments are
translated into calls to getter or setter methods, which can be
overridden fairly easily, and which can modify the object 'in place'.
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Jul 18 '05 #4

<us**@domain.invalid> wrote in message
news:5e******************************@news.teranew s.com...
Is it possible to override assignment, the
way that '+' can be overridden for example?

In Python, = is *not* an operator. It is more like a statement


Jul 18 '05 #5

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

Similar topics

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...
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...
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 + ...
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...
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....
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...
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;
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 {...
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...
by: Anton Mellit | last post by:
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...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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...
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...
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...

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.