overriding equals operation

Discussion in 'Python' started by Pradipto Banerjee, Oct 16, 2012.

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

    Currently, I have the following:

    ----

    class myclass(object):
    def __init__(self, name='')
    self.name = name

    def copy(self):
    newvar = myclass(self.name)
    return newvar

    def __eq__(self, other):
    if instance(other, myclass):
    return self == other.copy()
    return NotImplemented
    ----

    Now if I try:

    >>> a=myclass()
    >>> a.name = 'test'
    >>> b=a
    >>> b.name

    'test'
    >>> b.name = 'test2'
    >>> a.name

    'test2'

    I wanted b=a to make a new copy of "a", but then when I assigned b.name = 'test2', even a.name became 'test2'.

    How can I rectify my code to make the __eq__() behave like copy()?

    Thanks


    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. Any offer or solicitation of an investment in any of the Funds may be made only by delivery of such Funds confidential offering materials to authorized prospective investors. An investment in any of the Funds is not suitable for all investors. No representation is made that the Fundswill or are likely to achieve their objectives, or that any investor will or is likely to achieve results comparable to those shown, or will make anyprofit at all or will be able to avoid incurring substantial losses. Performance results are net of applicable fees, are unaudited and reflect reinvestment of income and profits. Past performance is no guarantee of future results. All financial data and other information are not warranted as to completeness or accuracy and are subject to change without notice.

    Any comments or statements made herein do not necessarily reflect those of Ada Investment Management LP and its affiliates. This transmission may contain information that is confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is strictly prohibited. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format.
     
    Pradipto Banerjee, Oct 16, 2012
    #1
    1. Advertising

  2. 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
     
    Thomas Rachel, Oct 16, 2012
    #2
    1. Advertising

  3. Pradipto Banerjee

    Nobody Guest

    On Tue, 16 Oct 2012 08:51:46 -0500, Pradipto Banerjee wrote:

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


    It cannot be done.

    Name binding ("variable = value") is a language primitive; it is not
    delegated to the object on the LHS (if there even is one; the name doesn't
    have to exist at the point that the statement is executed).

    You'll have to change the syntax to something which is delegated to
    objects, e.g. "a[:] = b" will call "a.__setitem__(slice(None), b)"
     
    Nobody, Oct 16, 2012
    #3
  4. Pradipto Banerjeeæ–¼ 2012å¹´10月16日星期二UTC+8下åˆ9時59分05秒寫é“:
    > 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 copyof "b", but I can't get it right.
    >
    >
    >
    > Currently, I have the following:
    >
    >
    >
    > ----
    >
    >
    >
    > class myclass(object):
    >
    > def __init__(self, name='')
    >
    > self.name = name
    >
    >
    >
    > def copy(self):
    >
    > newvar = myclass(self.name)
    >
    > return newvar
    >
    >
    >
    > def __eq__(self, other):
    >
    > if instance(other, myclass):
    >
    > return self == other.copy()
    >
    > return NotImplemented
    >
    > ----
    >
    >
    >
    > Now if I try:
    >
    >
    >
    > >>> a=myclass()

    >
    > >>> a.name = 'test'

    >
    > >>> b=a

    >

    What you really want is b=a.copy()
    not b=a to disentangle two objects.

    __eq__ is used in the comparison operation.



    > >>> b.name

    >
    > 'test'
    >
    > >>> b.name = 'test2'

    >
    > >>> a.name

    >
    > 'test2'
    >
    >
    >
    > I wanted b=a to make a new copy of "a", but then when I assigned b.name= 'test2', even a.name became 'test2'.
    >
    >
    >
    > How can I rectify my code to make the __eq__() behave like copy()?
    >
    >
    >
    > Thanks
    >
    >
    >
    >
    >
    > 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. Any offer or solicitation of an investment in any of the Funds may be made only by delivery of such Funds confidential offering materials to authorized prospective investors. An investment in any of the Fundsis not suitable for all investors. No representation is made that the Funds will or are likely to achieve their objectives, or that any investor will or is likely to achieve results comparable to those shown, or will make any profit at all or will be able to avoid incurring substantial losses. Performance results are net of applicable fees, are unaudited and reflect reinvestment of income and profits. Past performance is no guarantee of future results. All financial data and other information are not warranted as tocompleteness or accuracy and are subject to change without notice.
    >
    >
    >
    > Any comments or statements made herein do not necessarily reflect those of Ada Investment Management LP and its affiliates. This transmission may contain information that is confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient,you are hereby notified that any disclosure, copying, distribution, or useof the information contained herein (including any reliance thereon) is strictly prohibited. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whetherin electronic or hard copy format.
     
    88888 Dihedral, Oct 17, 2012
    #4
  5. Pradipto Banerjeeæ–¼ 2012å¹´10月16日星期二UTC+8下åˆ9時59分05秒寫é“:
    > 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 copyof "b", but I can't get it right.
    >
    >
    >
    > Currently, I have the following:
    >
    >
    >
    > ----
    >
    >
    >
    > class myclass(object):
    >
    > def __init__(self, name='')
    >
    > self.name = name
    >
    >
    >
    > def copy(self):
    >
    > newvar = myclass(self.name)
    >
    > return newvar
    >
    >
    >
    > def __eq__(self, other):
    >
    > if instance(other, myclass):
    >
    > return self == other.copy()
    >
    > return NotImplemented
    >
    > ----
    >
    >
    >
    > Now if I try:
    >
    >
    >
    > >>> a=myclass()

    >
    > >>> a.name = 'test'

    >
    > >>> b=a

    >

    What you really want is b=a.copy()
    not b=a to disentangle two objects.

    __eq__ is used in the comparison operation.



    > >>> b.name

    >
    > 'test'
    >
    > >>> b.name = 'test2'

    >
    > >>> a.name

    >
    > 'test2'
    >
    >
    >
    > I wanted b=a to make a new copy of "a", but then when I assigned b.name= 'test2', even a.name became 'test2'.
    >
    >
    >
    > How can I rectify my code to make the __eq__() behave like copy()?
    >
    >
    >
    > Thanks
    >
    >
    >
    >
    >
    > 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. Any offer or solicitation of an investment in any of the Funds may be made only by delivery of such Funds confidential offering materials to authorized prospective investors. An investment in any of the Fundsis not suitable for all investors. No representation is made that the Funds will or are likely to achieve their objectives, or that any investor will or is likely to achieve results comparable to those shown, or will make any profit at all or will be able to avoid incurring substantial losses. Performance results are net of applicable fees, are unaudited and reflect reinvestment of income and profits. Past performance is no guarantee of future results. All financial data and other information are not warranted as tocompleteness or accuracy and are subject to change without notice.
    >
    >
    >
    > Any comments or statements made herein do not necessarily reflect those of Ada Investment Management LP and its affiliates. This transmission may contain information that is confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient,you are hereby notified that any disclosure, copying, distribution, or useof the information contained herein (including any reliance thereon) is strictly prohibited. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whetherin electronic or hard copy format.
     
    88888 Dihedral, Oct 17, 2012
    #5
  6. On 17/10/2012 05:16, 88888 Dihedral wrote:
    > What you really want is b=a.copy()
    > not b=a to disentangle two objects.
    >
    > __eq__ is used in the comparison operation.
    >


    The winner Smartest Answer by a Bot Award 2012 :)

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Oct 17, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward A Thompson
    Replies:
    4
    Views:
    547
    Tony Morris
    Feb 11, 2004
  2. david ullua
    Replies:
    13
    Views:
    684
  3. raan
    Replies:
    2
    Views:
    462
  4. Dave Angel

    Re: overriding equals operation

    Dave Angel, Oct 16, 2012, in forum: Python
    Replies:
    0
    Views:
    136
    Dave Angel
    Oct 16, 2012
  5. Dwight Hutto

    Re: overriding equals operation

    Dwight Hutto, Oct 17, 2012, in forum: Python
    Replies:
    0
    Views:
    178
    Dwight Hutto
    Oct 17, 2012
Loading...

Share This Page