2

In Java, using the close() method frees resources by removing out of scope objects. I am confused as to what is the Python equivalent for this?

  • Does Python's garbage collector automatically do this?
  • Is it the del method?
  • Should I use the with statement?

Any help will be appreciated.

Edit: My goal is to explicitly remove class objects when desired, not file objects.

Sajid
  • 97
  • 2
  • 7
  • http://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used does this help? – AgataB Aug 03 '16 at 13:43
  • @AgataB Not really – Sajid Aug 03 '16 at 13:45
  • Do you have a specific use case that requires this? Can you elaborate on it? Python use wouldn't typically involve zeroing out instances (what you call class objects) manually. **weakref** might be of interest to avoid them sticking around. **with** is **not** an appropriate construct because it operates **within the context of** an instance - it's intended to free **resources** held by an instance. I've never seen any seriously written Python code use **gc** directly - not to say it should never be done, but it's hackish wo a very good reason. – JL Peyret Aug 03 '16 at 17:23

3 Answers3

0

Should I use the with statement?

Yes, according to the documentation:

It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:

iCart
  • 2,179
  • 3
  • 27
  • 36
0

You will find your answer here: place gc.collect() before your code.

Community
  • 1
  • 1
datahero
  • 101
  • 5
  • Does this also remove unused class objects? – Sajid Aug 03 '16 at 13:46
  • For that, the following discussion might be of interest: http://stackoverflow.com/questions/693070/how-can-you-find-unused-functions-in-python-code. – datahero Aug 03 '16 at 13:51
  • It seems that you would need to use additional tools (pyflakes and vulture). – datahero Aug 03 '16 at 13:52
  • 1
    That is not actually what I want, I should have phrased my question better. What I need to do, is to "close" objects that I am no longer going to use, explicitly using a statement. – Sajid Aug 03 '16 at 13:57
  • In that case, there is a similar method in Python which is also named close(): 9https://docs.python.org/3/tutorial/inputoutput.html. Is this more what you are looking for? – datahero Aug 03 '16 at 14:09
  • 1
    Yes, I am looking for similar functionality like that, but for class objects, not to close files. – Sajid Aug 03 '16 at 14:17
  • Hopefully then, this will be for you: http://stackoverflow.com/questions/865115/how-do-i-correctly-clean-up-a-python-object. Apparently the "with' statement is a good practice. With "del", objects are kept in memory for some time... – datahero Aug 03 '16 at 14:23
0

ICart's comment is spot on for many different situations. However, regarding the creation and disposal of objects in general, I think this other thread provides a pretty good explanation of how python handles their objects (reference counts), and where garbage collection comes in.

Community
  • 1
  • 1
Kenneth Lim
  • 75
  • 2
  • 9