Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89830
| References | <387ac520-9da3-411a-a3b8-b326e1e8a2c0@googlegroups.com> <20150502151723.3a36e33b@bigbox.christie.dr> <CALwzidnnxU7N0Kgx-eMM5Lq4th6xoRggXLQrsS7fnj9wJnEHOQ@mail.gmail.com> |
|---|---|
| Date | 2015-05-03 15:46 +1000 |
| Subject | Re: Inner workings of this Python feature: Can a Python data structure reference itself? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.47.1430632020.12865.python-list@python.org> (permalink) |
On Sun, May 3, 2015 at 2:43 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote: > On Sat, May 2, 2015 at 2:17 PM, Tim Chase <python.list@tim.thechases.com> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-02 13:02 -0700
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Tim Chase <python.list@tim.thechases.com> - 2015-05-02 15:17 -0500
Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-02 14:07 -0700
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Cecil Westerhof <Cecil@decebal.nl> - 2015-05-02 23:06 +0200
Re: Inner workings of this Python feature: Can a Python data structure reference itself? MRAB <python@mrabarnett.plus.com> - 2015-05-03 00:07 +0100
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Tim Chase <python.list@tim.thechases.com> - 2015-05-02 20:57 -0500
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-02 22:29 -0600
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Tim Chase <python.list@tim.thechases.com> - 2015-05-02 15:10 -0500
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Terry Reedy <tjreedy@udel.edu> - 2015-05-02 19:17 -0400
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-05-03 21:10 +1000
Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 05:59 -0700
Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 06:05 -0700
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Chris Angelico <rosuav@gmail.com> - 2015-05-03 23:08 +1000
Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 08:46 -0700
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-02 22:43 -0600
Re: Inner workings of this Python feature: Can a Python data structure reference itself? Chris Angelico <rosuav@gmail.com> - 2015-05-03 15:46 +1000
Re: Inner workings of this Python feature: Can a Python data structure reference itself? vasudevram <vasudevram@gmail.com> - 2015-05-03 03:52 -0700
csiph-web