[Tutor] Memory and functions

2011-12-18 Thread Charles Becker
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).

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

Thanks!
Charles
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Memory and functions

2011-12-18 Thread Steven D'Aprano

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Memory and functions

2011-12-18 Thread Peter Otten
Charles Becker wrote:

 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).

Every object in CPython has a counter to keep track how many times it is 
referenced. When this counter drops to zero the object is destroyed and the 
memory is available for reuse. This is simple and works most of the time 
except when there are reference cycles. Consider:

class A: 
pass
a = A()
b = A()
a.other = b
b.other = a
del a
del b

There is now no way for your program to reach a or b, but the reference 
counter of a is still one because it is referenced by b as b.other, and the 
reference counter of b is still one because it's referenced by a. 
Therefore CPython has another mechanism called garbage collection as a 
backup that periodically searches for reference cycles.

While the specific mechanism described above is only implemented in CPython 
all variants (pypy, jython, ironpython) guarantee that you don't have to 
bother about object lifetime.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor