Re: overriding equals operation

Discussion in 'Python' started by Dwight Hutto, Oct 17, 2012.

  1. Dwight Hutto

    Dwight Hutto Guest

    On Tue, Oct 16, 2012 at 9:51 AM, 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.
    >
    > Currently, I have the following:
    >
    > ----
    >
    > class myclass(object):
    > def __init__(self, w3kschoolsname='')
    > 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
    >
    >
    >


    If I'm understanding correctly(quick look at it), then write a new py
    file and __import__ it if I'm remember correctly.

    Use a secondary file to rewrite the existing nature of the python code
    file, then import it, and utilize the remade secondary py file for
    your usage..
    --
    Best Regards,
    David Hutto
    CEO: http://www.hitwebdevelopment.com
     
    Dwight Hutto, Oct 17, 2012
    #1
    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:
    538
    Tony Morris
    Feb 11, 2004
  2. david ullua
    Replies:
    13
    Views:
    674
  3. raan
    Replies:
    2
    Views:
    456
  4. Pradipto Banerjee

    overriding equals operation

    Pradipto Banerjee, Oct 16, 2012, in forum: Python
    Replies:
    5
    Views:
    157
    Mark Lawrence
    Oct 17, 2012
  5. Dave Angel

    Re: overriding equals operation

    Dave Angel, Oct 16, 2012, in forum: Python
    Replies:
    0
    Views:
    135
    Dave Angel
    Oct 16, 2012
Loading...

Share This Page