Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed3a.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.039 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'python,': 0.02; 'cpython': 0.05; 'subject:Python': 0.06; 'memory.': 0.07; 'lst': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'cleaned': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'once.': 0.16; 'subject: \n ': 0.16; 'usage,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; "python's": 0.19; 'meant': 0.20; 'code,': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'necessary.': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'tim': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; 'code': 0.31; 'breaking': 0.31; 'bunch': 0.31; 'chase': 0.31; 'advice': 0.35; 'except': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'consistent': 0.36; 'subject:data': 0.36; 'done': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'sometimes': 0.38; 'pm,': 0.38; 'either': 0.39; 'major': 0.40; 'even': 0.60; 'ian': 0.60; 'manually': 0.60; 'subject:Can': 0.60; 'worry': 0.60; 'most': 0.60; 'break': 0.61; 'strictly': 0.61; 'simple': 0.61; "you're": 0.61; 'kind': 0.63; 'such': 0.63; 'pick': 0.64; 'more': 0.64; 'believe': 0.68; 'containing': 0.69; 'guaranteed': 0.75; 'potentially': 0.81; 'subject:this': 0.83; '2015': 0.84; '3.4': 0.84; 'whereas': 0.91; 'to:none': 0.92 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:cc :content-type; bh=IKkFv+pp7h6YqSnPpT9QVUie/AA9bpEDkDPuHuu1F0w=; b=wIyy3TMKzybBrF0fTvxh2pQQSyGLxOlTFPuQrxyUXDnF7I9OW1dL+4q1rloGTLX/Ew lB5lRFUVGv6tvwFdwgApQC2jpLh7yzKME8B+gAXWhyJDAFLkYoHiwaBPWikDAt3m8xj1 XJequq2I1KipVkiQ+ssTbllIExVzIgqtbFHvZrv91k7CrM5Mn6o7nAp6rttC6knJSY+6 AoTqCe1wWTO0+bzRt7sRXXQyYNcs2shBOFciASOh3YjTH5S1ZqlafXWlVSLZYFDgz+ww IejuGwDqLziEwDtdsNo3664s4Bs496xJBLfa3MWZtCFBga8WUSII2qBYIWQ1lr74Go8c Amrg== MIME-Version: 1.0 X-Received: by 10.107.160.202 with SMTP id j193mr21307199ioe.43.1430632012226; Sat, 02 May 2015 22:46:52 -0700 (PDT) In-Reply-To: References: <387ac520-9da3-411a-a3b8-b326e1e8a2c0@googlegroups.com> <20150502151723.3a36e33b@bigbox.christie.dr> Date: Sun, 3 May 2015 15:46:52 +1000 Subject: Re: Inner workings of this Python feature: Can a Python data structure reference itself? From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430632020 news.xs4all.nl 2962 [2001:888:2000:d::a6]:49705 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89830 On Sun, May 3, 2015 at 2:43 PM, Ian Kelly wrote: > On Sat, May 2, 2015 at 2:17 PM, Tim Chase wrote: >> If you know that you're creating such cyclical structures, it's best >> to manually unlink them before freeing them: >> >> lst = [] >> lst.append(lst) # create the cycle >> lst[:] = [] # break the cycle >> # or lst.remove(lst) # though this takes more care >> del lst > > In general, this shouldn't be necessary. I believe that reference > cycles are guaranteed to be cleaned up in all major implementations of > Python, except that in CPython prior to version 3.4 reference cycles > containing objects with finalizers would not be collected. So the > better advice would be "don't use finalizers in reference cycles if > you need compatibility with Python 3.3 or earlier." It's not strictly necessary, but you can help things along by breaking the cycle - if you're doing this kind of thing in a loop, breaking the cycle will most likely give more consistent memory usage, whereas the GC will potentially pick up a bunch of them all at once. In simple applications, it's not a big deal either way. Just write your code, and let Python worry about memory. But if it doesn't cost you much to break the loop, you may as well do it Sometimes it even improves code clarity - showing that you're definitely done with this thing now, even though in theory you might still be iterating over it. Just don't warp your code around memory usage, because that's not what Python's meant for. :) ChrisA