Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: Python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'class,': 0.07; 'rewrite': 0.07; 'try:': 0.07; 'python': 0.09; '"a"': 0.09; 'behave': 0.09; 'currently,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'file,': 0.15; 'copy(self):': 0.16; 'it),': 0.16; 'oct': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'pfxlen:0': 0.17; 'define': 0.20; 'trying': 0.21; 'import': 0.21; 'received:209.85.216.46': 0.21; 'cc:2**0': 0.23; 'statement': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '----': 0.27; 'am,': 0.27; 'right.': 0.27; 'message- id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'correctly.': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'self': 0.34; 'thanks': 0.34; 'nature': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'wanted': 0.36; 'should': 0.36; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'skip:n 10': 0.63; "'test'": 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=8tqcqwkZf3muKsTNWvFoGdwZLFRtf4OAHBYxAKgDgpw=; b=XUkK2VdEdyyvmch/1qC3YiIceQbv1vrTOFHEJU3fXlikGX5xeYnD36dUpAoq5J+g2f cRXIvR+E0R2796GQs8U3FJVD66n1bqjMx8kfEoPtKfov55773DoPwKpLlzT6mXZfaXFX bn3OwrJDG0Z2BZo0bL8oJkLw61bd8w5OmmS0ybtUnk1NDfp4VeTc6AjAc7eudUgUGAfh uoqTwlRwlWGY7jbxoxcHtL9DZhpKT+AypaBiLEXi18xjc2ox2TrDmIxXFLDvLJgrOwqk t0rrKEmNN0uatZPAIP0PFFhnum8Cwg0R5IaiiTn6OutgmB7hO95QCheFpnhkZfX5Setn 0ESw== MIME-Version: 1.0 In-Reply-To: <76D03718A3233B4C8CC236C169B535B5A23DFDFB43@AUSP01VMBX08.collaborationhost.net> References: <76D03718A3233B4C8CC236C169B535B5A23DFDFB43@AUSP01VMBX08.collaborationhost.net> Date: Tue, 16 Oct 2012 21:51:54 -0400 Subject: Re: overriding equals operation From: Dwight Hutto To: Pradipto Banerjee Content-Type: text/plain; charset=ISO-8859-1 Cc: "Python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350438716 news.xs4all.nl 6875 [2001:888:2000:d::a6]:59623 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31444 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