Open guettli opened 8 years ago
since I think the open file gets closed as soon as the variable gets garbage collected.
I don't know exactly. You may be right but I don't know whether Python will call close() when that line of code (or scope?) is over (because the opened file object after that no longer "exists").
Could it be that the file handler (internally) is kept open for a bit longer - until the next garbage collection or until the end of the process?
If you ensure that close() is called explicitly then at least it removes this uncertainty for anyone reading the code. Without it you'd need to be aware of how Python does this behind the scenes.
I often use this:
According to your page "python improvements" this should be avoided, since the file does not get closed explicitly.
I don't understand it, since I think the open file gets closed as soon as the variable gets garbage collected.
What do you think? Is above line clean code?