overriding equals operation
Thomas Rachel
nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Tue Oct 16 10:07:04 EDT 2012
Am 16.10.2012 15:51 schrieb Pradipto Banerjee:
> I am trying to define class, where if I use a statement a = b, then instead of "a" pointing to the same instance as "b", it should point to a copy of "b", but I can't get it right.
This is not possible.
> Currently, I have the following:
>
> ----
>
> class myclass(object):
Myclass or MyClass, see http://www.python.org/dev/peps/pep-0008/.
> def __eq__(self, other):
> if instance(other, myclass):
> return self == other.copy()
> return NotImplemented
This redefines the == operator, not the = operator.
It is not possible to redefine =.
One way could be to override assignment of a class attribute. But this
won't be enough, I think.
Let me explain:
class MyContainer(object):
@property
def content(self):
return self._content
@content.setter
def content(self, new):
self._content = new.copy()
Then you can do:
a = MyClass()
b = MyContainer()
b.content = a
print b.content is a # should print False; untested...
But something like
a = MyClass()
b = a
will always lead to "b is a".
> This communication is for informational purposes only. It is not
> intended to be, nor should it be construed or used as, financial,
> legal, tax or investment advice or an offer to sell, or a
> solicitation of any offer to buy, an interest in any fund advised by
> Ada Investment Management LP, the Investment advisor.
What?
Thomas
More information about the Python-list
mailing list