473,322 Members | 1,379 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,322 software developers and data experts.

overriding '=' operator?

Hi,

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

Thanks,

Toby

Jul 18 '05 #1
4 14299
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

or

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...
Hi,

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
Thanks,

Toby

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

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
keyword.

tjr

Jul 18 '05 #5

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...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.