Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out2.kabelfoon.nl!newsfeed.kabelfoon.nl!xindi.nntp.kabelfoon.nl!feed.xsnews.nl!border-2.ams.xsnews.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(at': 0.03; 'happily': 0.07; 'python': 0.08; 'cleaned': 0.09; 'reference)': 0.09; 'thinking,': 0.09; 'subject:python': 0.11; 'am,': 0.12; 'def': 0.14; '(assuming': 0.16; 'expired': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'perhaps,': 0.16; 'subject:between': 0.16; 'subject:classes': 0.16; 'wrote:': 0.18; 'received:209.85.210.174': 0.18; 'received:mail- iy0-f174.google.com': 0.18; "doesn't": 0.23; 'least,': 0.23; 'header:In-Reply-To:1': 0.23; 'variable': 0.24; 'object,': 0.24; 'explicitly': 0.28; 'message-id:@mail.gmail.com': 0.29; 'object.': 0.30; 'nov': 0.31; 'objects': 0.32; 'to:addr:python-list': 0.32; 'it.': 0.33; 'named': 0.33; 'object': 0.35; 'fri,': 0.36; 'skip:" 10': 0.36; 'reference': 0.37; 'but': 0.37; 'talk': 0.37; 'references': 0.38; 'received:google.com': 0.38; 'some': 0.38; 'else': 0.38; 'received:209.85': 0.38; 'difficult': 0.38; 'except': 0.39; "it's": 0.39; 'subject:: ': 0.39; 'change': 0.39; 'to:addr:python.org': 0.39; 'skip:_ 10': 0.40; 'received:209': 0.40; 'really': 0.40; 'your': 0.61; '2011': 0.62; '11,': 0.68; 'subject:The': 0.72; 'lose': 0.82; 'zhang': 0.84; "everything's": 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=NaaU8gf5vrWW7IPqF9MP11MTBABkUdgciK2Dixocvjc=; b=DAxYMTfgM2e4klSFDJwKJ14QpPdTXaBpekZkADMn+SrgH5ou3kKMPPhslNi2UTTrCm 5CPg+DND5KF6p60Hvz1DnaEgSfwhOBTABETzt2raMqzfCzEjXEuyPf91LfSbeKLGkGWT 7BUr2pl2bAOP3Qy9vAXir/lzHHXYCBylg6oOI= MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 11 Nov 2011 01:09:07 +1100 Subject: Re: The python implementation of the "relationships between classes". From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1320934151 news.xs4all.nl 6974 [2001:888:2000:d::a6]:36631 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15537 On Fri, Nov 11, 2011 at 12:58 AM, Jerry Zhang wr= ote: > Cls_a: > =A0 =A0 def __init__(self): > =A0 =A0 =A0 =A0 self.at1 =3D 1 > Cls_b: > =A0 =A0 def __init__(self): > =A0 =A0 =A0 =A0 self.com1 =3D Cls_a() > =A0 =A0 def __del__(self): > =A0 =A0 =A0 =A0 del self.com1 > Is it a right implementation for composition? Yes, except that you don't need to explicitly del it. Python (at least, CPython) is reference-counted; your Cls_b object owns a reference to the Cls_a object, so (assuming nothing else has a reference) that Cls_a will be happily cleaned up when the Cls_b is. Python doesn't really talk about "composition" etc. It's much simpler: everything's an object, and you have references to that object. A named variable is a reference to some object. A member on an object is, too. Whenever an object is expired, all objects that it references lose one reference, and if that was the sole reference, those objects get expired too. It's a change of thinking, perhaps, but not a difficult one in my opinion; and it's so easy to work with when you grok it. ChrisA