[Tutor] Memory and functions

Steven D'Aprano steve at pearwood.info
Sun Dec 18 13:02:10 CET 2011


Charles Becker wrote:
> I have a rather obscure question, and not sure where in the docs to find an answer like this.
> 
> For functions that return values (ie the list method pop): If you don't assign the returned value to anything does it still end up residing in memory?  Just seems like this could be a potential problem to watch out for when memory usage is an issue (probably not most of the time).

Python is a modern language with a garbage collector. That means that behind 
the scenes Python deletes objects which are no longer in use. So the short 
answer is, yes, if you don't assign the returned value, it will be deleted by 
the garbage collector and you don't normally have to worry about it.


> Also, where can I begin to find these 'lower level' answers in the docs?

Mostly in the source code. Otherwise, scattered all over the documentation: it 
depends on what you consider "lower level".

If you're familiar with C, or just curious, you might read this part:

http://docs.python.org/extending/extending.html#reference-counts


-- 
Steven


More information about the Tutor mailing list