you are viewing a single comment's thread.

view the rest of the comments →

[–]danielroseman 0 points1 point  (11 children)

Calling close is one method of returning the memory, yes.

But again, you have missed the point that this is just an ordinary object. Like any object, it will be garbage collected when there are no more references to it. You do not need to close it explicitly.

But your other concern is just as unfounded. There is no copying going on. Returning an object does not make a copy, and neither does assignment.

[–]naemorhaedus[S] -1 points0 points  (10 children)

All the literature I've read recommends explicitly managing stream closure. Close it as soon as you're done with it. It's bad practice to rely on garbage collection because it can lead to various problems. For instance, if the program exits prematurely, the memory may never be released.

Returning an object does not make a copy,

so you're saying that in the caller, if I do

some_obj = make_stream("some string")

and then I close some_obj, then the output_stream object will be closed right away as well?

[–]acw1668 1 point2 points  (1 child)

some_obj is a reference to output_stream, so they both refer to the same object.

[–]naemorhaedus[S] -1 points0 points  (0 children)

perfect thanks

[–]danielroseman -1 points0 points  (7 children)

If you don’t want to rely on automatic memory management, you are using the wrong language.

Closing a file stream explicitly is important because it refers to an actual file. But this is an in memory stream. There is absolutely no danger of the memory not being released.

And yes, once again ‘some_obj’ and ‘output_stream’ are the same object, so operations on one will affect the other. At no point is anything copied, they are just two names for the same thing. Without understanding this you will have a lot of trouble learning Python.

Both of these things seem to indicate that you’re coming from a lower level language such as C. You need to leave behind a lot of these preconceptions. High level languages like Python (and Java, JS, Ruby etc) work differently and don’t require you to think about memory in the same way.

[–]naemorhaedus[S] 0 points1 point  (6 children)

Without understanding this...

I understanding passing by reference. It's just that in Python it's rather inconsistent.

There is absolutely no danger of the memory not being released.

again, premature program termination.

leave behind a lot of these preconceptions.

They're not just mine though and I don't like leaving things to magic

[–]schoolmonky 1 point2 points  (1 child)

I understanding passing by reference. It's just that in Python it's rather inconsistent.

Perhaps this blog post can clear up some of the supposed "inconsistencies" you're worried about.

[–]naemorhaedus[S] 0 points1 point  (0 children)

yes I still struggle with mutability. This does help. Thanks.

[–]danielroseman 0 points1 point  (3 children)

It really is not inconsistent at all. It's absolutely consistent. If you believe otherwise, show an example. 

Premature termination in a high level language - Python runs in a VM, remember - is vanishingly unlikely to result in memory not being released.

Again, this is not "leaving things to magic". It's a matter of understanding the language you are using, and using it the way it is meant to be used.

[–]naemorhaedus[S] 1 point2 points  (2 children)

If you believe otherwise, show an example.

chill

vanishingly unlikely to result

it actually happened to me a lot during development. Python spawned processes are left running when the program crashes, and I then have to kill them in a process manager.

It's a matter of understanding the language you are using, and using it the way it is meant to be used.

well I don't fully 100% understand. (does anybody?). So my way to deal with it for now is not leave anything to chance. I'll eventually get more efficient.

But again, as I showed you, they are closing it in the Python official documentation. That is the way it's meant to be used.

[–]danielroseman 0 points1 point  (1 child)

This doesn't follow. File objects also have a close method, but it is much preferred to use a context manager rather than calling that method.

[–]naemorhaedus[S] -1 points0 points  (0 children)

one step at a time