By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,034 Members | 2,000 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,034 IT Pros & Developers. It's quick & easy.

How do I overload 'equals'?

P: n/a
Hi there,

I just figured out how to use __add__() to overload the "+" operator
for objects of a class.

According to googel queries, I see other functions available to me for
overloading other operators:

__mul__(), __sub__(), ...

I was hoping to overload "=" or use some string to do assignment with
side effects.

For example,

obj1 = ClassX('green')
obj2 = ClassX('red')

obj1 superEqual obj2

I'd like to be able to control the behavior of the superEqual operator
such that it does assignment with side effects.

For example, maybe it would do this:

obj = obj2
incrementEqualityCounter()
addAuditRecordToDB()

How do I do this in Python?

-moi
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Equis Uno said the following on 02/02/2004 11:52 AM:
Hi there,

I just figured out how to use __add__() to overload the "+" operator
for objects of a class.

According to googel queries, I see other functions available to me for
overloading other operators:

__mul__(), __sub__(), ...

I was hoping to overload "=" or use some string to do assignment with
side effects.

For example,

obj1 = ClassX('green')
obj2 = ClassX('red')

obj1 superEqual obj2

I'd like to be able to control the behavior of the superEqual operator
such that it does assignment with side effects.

For example, maybe it would do this:

obj = obj2
incrementEqualityCounter()
addAuditRecordToDB()

How do I do this in Python?


Overloading operators is explained in
http://python.org/doc/2.3.3/lib/modu...r.html#l2h-490

Maybe you are looking for __eq__? After overloading it, you may
say:
if obj1 == obj2:
...
From what you wrote I think you want to overload the assign operator
(single =) . This is not supported in Python directly, but you may take
a look at descriptors:
http://users.rcn.com/python/download/Descriptor.htm

Regards
Mirko
--
Jul 18 '05 #2

P: n/a
On Monday 02 February 2004 11:36 am, Mirko Zeibig wrote:
Equis Uno said the following on 02/02/2004 11:52 AM:
Hi there,

I just figured out how to use __add__() to overload the "+" operator
for objects of a class.

According to googel queries, I see other functions available to me for
overloading other operators:

__mul__(), __sub__(), ...

I was hoping to overload "=" or use some string to do assignment with
side effects.

For example,

obj1 = ClassX('green')
obj2 = ClassX('red')

obj1 superEqual obj2

I'd like to be able to control the behavior of the superEqual operator
such that it does assignment with side effects.

For example, maybe it would do this:

obj = obj2
incrementEqualityCounter()
addAuditRecordToDB()

How do I do this in Python?


Overloading operators is explained in
http://python.org/doc/2.3.3/lib/modu...r.html#l2h-490

Maybe you are looking for __eq__? After overloading it, you may
say:
if obj1 == obj2:
...
From what you wrote I think you want to overload the assign operator
(single =) . This is not supported in Python directly, but you may take
a look at descriptors:
http://users.rcn.com/python/download/Descriptor.htm


Descriptors, or just defining good old fashioned __getattr__
(http://www.python.org/doc/current/re...e-access.html), can only
overload assignment to attributes.

Changing the meaning of "=", as I believe you want to do, is the kind of
change to the syntax of the language that Python by design does not allow in
order to maintain consistency across different people's code (and no doubt
for lots of other good reasons). If you really want to do this perhaps you'd
be better off using Io <wink>. Otherwise you'll have to settle for defining
a function "superEqual" (or better "superAssign", since"=" is an assignment
operator not an equality operator.)

James
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
Jul 18 '05 #3

P: n/a
ok,

So I can't overload '='.
Fair enough.

How do I create/define an operator for a class of objects?

For example,

I'd like these statements:

aHouse = makeAhouse()
aHouse superAssign myHouse # use the superAssign operator

to fill aHouse with all the objects inside myHouse
and then call an arbitray method:
myHouse.log('aHouse has a copy of your stuff')

Is this possible?

-moi
Jul 18 '05 #4

P: n/a
> aHouse = makeAhouse()
aHouse superAssign myHouse # use the superAssign operator

to fill aHouse with all the objects inside myHouse
and then call an arbitray method:
myHouse.log('aHouse has a copy of your stuff')

Is this possible?


First of all, write the operator as simple function with two arguments, your
aHouse and myHouse:

def init_house(aHouse, myHouse):
aHouse.inhabitants = myHouse.inhabintants
....

Now if you actually have different functions, depending on the actual types
you use, you could go for multimethod-dispatch and create a HouseAssigner
like this:

class HouseAssigner(multimethods.Dispatch):
def __init__(self):
multimethods.Dispatch.__init__(_)
_.add_rule((AHouse, MyHouse), _.init_house)

I assumed that aHouse is of tpye AHouse, and myHouse of MyHouse

Now you can create an instance of HouseAssigner and use that to perform the
actual assignment:

ha = HousAssigner()
ha(aHouse, myHouse)

Now for the operator-stuff: My c++-skills are somewhat rusted (something I'm
not sure if to be glad of or not), so I don't remember how to exactly
declare a custom assignment-operator.

However, I think that you are after a thing here that I personally would
consider as bad style: Usually, polymorphism is used to write code that is
not interested in details of some actual object, but works on abstract
concepts. An example would be a ParkController working on Car-objects, but
you feed it with Porsche, Mercedes and BMW-objects (which inherit from Car,
of course). Still the actual car knows about its unique features.

Introducing an assignment operator like you want it to have now acutally
performs willingly a slicing-operation - the object forgots something about
what its capable/consisting of. I don't see any reason for that - it might
even lead to severe problems, as accidential slicing in c++ does.

So - maybe you could fill in what actual use-case you have for such a
behaviour.

Another thing to mention might be that assignment in python is different
from assignment in C/C++:

c = Car()

only means that the identifier c now points to an instance of Car - not that
c is of type car. So in the next line, you could say:

c = 10

Others have explained that behaviour better, you might find informations in
the documentation.

Regards,

Diez

Jul 18 '05 #5

P: n/a
Diez,

your info about the 'multimethod-dispatch House Assigner' is kewl.
It's not what I'm currently looking for but I may in the future.

I have no use case.

My motivation is to learn about the limitations and capability of Python.

We could call it a useless case.

I suspect that building an operator with un-obvious side effects
is bad programming style.

It's better to just use a simple function to do the assignment:
aHouse = superAssign (myHouse)

If I want to know what superAssign() does, I go read it.

I'd still like to build an arbitrary operator though.

-moi
"Diez B. Roggisch" <de************@web.de> wrote in message news:<bv*************@news.t-online.com>...
aHouse = makeAhouse()
aHouse superAssign myHouse # use the superAssign operator

to fill aHouse with all the objects inside myHouse
and then call an arbitray method:
myHouse.log('aHouse has a copy of your stuff')

Is this possible?


First of all, write the operator as simple function with two arguments, your
aHouse and myHouse:

def init_house(aHouse, myHouse):
aHouse.inhabitants = myHouse.inhabintants
....

Now if you actually have different functions, depending on the actual types
you use, you could go for multimethod-dispatch and create a HouseAssigner
like this:

class HouseAssigner(multimethods.Dispatch):
def __init__(self):
multimethods.Dispatch.__init__(_)
_.add_rule((AHouse, MyHouse), _.init_house)

I assumed that aHouse is of tpye AHouse, and myHouse of MyHouse

Now you can create an instance of HouseAssigner and use that to perform the
actual assignment:

ha = HousAssigner()
ha(aHouse, myHouse)

Now for the operator-stuff: My c++-skills are somewhat rusted (something I'm
not sure if to be glad of or not), so I don't remember how to exactly
declare a custom assignment-operator.

However, I think that you are after a thing here that I personally would
consider as bad style: Usually, polymorphism is used to write code that is
not interested in details of some actual object, but works on abstract
concepts. An example would be a ParkController working on Car-objects, but
you feed it with Porsche, Mercedes and BMW-objects (which inherit from Car,
of course). Still the actual car knows about its unique features.

Introducing an assignment operator like you want it to have now acutally
performs willingly a slicing-operation - the object forgots something about
what its capable/consisting of. I don't see any reason for that - it might
even lead to severe problems, as accidential slicing in c++ does.

So - maybe you could fill in what actual use-case you have for such a
behaviour.

Another thing to mention might be that assignment in python is different
from assignment in C/C++:

c = Car()

only means that the identifier c now points to an instance of Car - not that
c is of type car. So in the next line, you could say:

c = 10

Others have explained that behaviour better, you might find informations in
the documentation.

Regards,

Diez

Jul 18 '05 #6

P: n/a
> My motivation is to learn about the limitations and capability of Python.

That is easy, Python can't do it.
We could call it a useless case.
So why bother? Oh yeah, "to learn about the limitations and capability
of Python".
I suspect that building an operator with un-obvious side effects
is bad programming style.
Of course. Building a comparison operator that creates arbitrary
attributes on an argument is one, of many, examples of bad programming
style.
It's better to just use a simple function to do the assignment:
aHouse = superAssign (myHouse)
I don't think that would do what you want. All that would do is assign
the name aHouse a reference to whatever is returned by
superAssign(myHouse). You aren't modifying what aHouse used to reference.
I'd still like to build an arbitrary operator though.


It is not possible for all operators. For a list of those operators
that you /can/ overload, check the 'operator' module.

- Josiah
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.