469,282 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

overriding '=' operator?

Hi,

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

Thanks,

Toby

Jul 18 '05 #1
4 12877
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Andrew Durdin | last post: by
3 posts views Thread by Cheng Mo | last post: by
11 posts views Thread by Michael | last post: by
6 posts views Thread by shoosh | last post: by
2 posts views Thread by franklini | last post: by
8 posts views Thread by Kenneth Baltrinic | last post: by
2 posts views Thread by linq936 | last post: by
1 post views Thread by SarahT | last post: by
2 posts views Thread by Anton Mellit | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.