11

Assume to not have any particular memory-optimization problem in the script, so my question is about Python coding style. That also means: is it good and common python practice to dereference an object as soon as whenever possible? The scenario is as follows.

Class A instantiates an object as self.foo and asks a second class B to store and share it with other objects. At a certain point A decides that self.foo should not be shared anymore and removes it from B.

Class A still has a reference to foo, but we know this object to be useless from now on. As foo is a relatively big object, would you bother to delete the reference from A and how? (e.g. del vs setting self.foo = None) How this decision influence the garbage collector?

Niccolò
  • 2,854
  • 4
  • 24
  • 38
  • 3
    Have you looked into [weak references](https://docs.python.org/2/library/weakref.html)? I think your example matches perfectly for that. – Bort Jul 24 '14 at 16:07
  • possible duplicate of [Would you prefer using del or reassigning to None (garbage collecting)](http://stackoverflow.com/questions/6693946/would-you-prefer-using-del-or-reassigning-to-none-garbage-collecting) – Michael0x2a Jul 24 '14 at 16:08
  • @Michael0x2a, thanks for the link, it answers very well to half of my question. The other half would be about the 'pythonic way': is it good and common practice to dereference an object any time possible? – Niccolò Jul 24 '14 at 16:20
  • @Niccolò Questions about "good practices" are opinion based, and don't fit well to this site's Q&A format. – BartoszKP Jul 24 '14 at 16:33
  • @BartoszKP do you have suggestions on how to improve the question? My question about "good practices" seeks two points. First, as you might know, python community is pretty strict/precise in the dichotomy derived by what is pythonic and good code. Does it have anything to say about how to handle this scenario? Second, on this website I've seen saying that «unusual means obscure means bad code» (e.g. http://stackoverflow.com/a/24435904/2389570). Would it be such a case if I do as in the question anytime possible? – Niccolò Jul 24 '14 at 18:43
  • @Niccolò First of all, don't ask about more than one thing in one question. Secondly, even if python community is *pretty* strict, the subject of readability and in particular being "pythonic" (whatever that means - from what I've seen, this term seems to be heavily overused) is not good for this site. Consider posting such question to Code Review. And last (but not least), I don't bother about such things in garbage collected environments until memory profiler indicates a particular problem. For which a concrete solution usually exists, instead of a set of subjective and fuzzy guidelines. – BartoszKP Jul 24 '14 at 19:27

3 Answers3

7

If, after deleting the attribute, the concept of accessing the attribute and seeing if it's set or not doesn't even make sense, use del. If, after deleting the attribute, something in your program may want to check that space and see if anything's there, use = None.

The garbage collector won't care either way.

Andrew Gorcester
  • 19,595
  • 7
  • 57
  • 73
4
del Blah

will reduce the reference count of Blah by one ... once there are no more references python will garbage collect it

self.foo = None 

will also reduce the reference count of Blah by one ... once there are no more references python will garbage collect it

neither method actually forces the object to be destroyed ... only one reference to it

* as a general rule of thumb I would avoid using del as it destroys the name and can cause other errors in your code if you try and reference it after that ...

in cPython (the "normal" python) this garbage collection happens very regularly

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

So far, in my experience with Python, I haven't had any problems with garbage collection. However, I do take precautions, not only because I don't want to bother with any unreferenced objects, but also for organization reasons as well.

To answer your questions specifically:

1) Yes, I would recommend deleting the object. This will keep your code from getting bulky and/or slow. This is an especially good decision if you have a long run-time for your code, even though Python is pretty good about garbage collection.

2) Either way works fine, although I would use del just for the sake of removing the actual reference itself.

3) I don't know how it "influences the garbage collector" but it's always better to be safe than sorry.

jaysoncopes
  • 805
  • 3
  • 13
  • 26
  • @JoranBeasley: Well, jaysoncopes is right that if you don't delete the reference the object will stick around and use up memory. However, I don't understand his claim that this will make a program "slow". A for #2, he said "Either way works fine" which is correct, but I don't understand "for the sake of removing the actual reference itself". – DanielSank Jul 24 '14 at 16:19
  • @DanielSank you are right ... I thought he was refering to `del` when he said deleting the object ... , aditionally having high memory consumption will definately make it slower ... my bad for commenting before really reading +1 to answer to make up for it since its basically right ... – Joran Beasley Jul 24 '14 at 16:22
  • @JoranBeasley: As DanielSank mentioned, if you don't delete the reference it will use up memory, no questions asked. As for the claiming that the program could run slow, you'd have to be using Python in a very inefficient way, which deleting these would be helping. It's definitely possible to make a python program run slow. And lastly, setting the reference to "None" doesn't remove it, it simply says that it is nothing. Deleting the reference will remove all possible ways of parsing through it, boosting efficiency. – jaysoncopes Jul 24 '14 at 16:24
  • 1
    @JoranBeasley Thanks. Sorry- didn't see your comment before I posted my explanation :P – jaysoncopes Jul 24 '14 at 16:25
  • @jaysoncopes see my response above ... also setting ref to None, does decrease the reference counter for `some_big_object` by 1 ... just like using `del` ... what I said about #1 was wrong because I wrongly assumed that when you said "deleteing the object" I read it as `del` – Joran Beasley Jul 24 '14 at 16:26