[Tutor] How python keeps track of objects

Alan Gauld alan.gauld at btinternet.com
Sun Nov 23 13:53:56 CET 2014

On 23/11/14 02:28, Mitch Raful wrote:
> If I have code similar to this:
> for object in objects:
>      do_something(object)
> def do_something(obj):
>       other_object = Class( obj.property)
>       other_object.method( arg1, arg2)
>      do_stuff here with other_object
>      if problem:
>           print( obj.property )
> My concern is that the for loop will wreak havoc with objects created in
> the function do_something.

Your question is a bit too generic to be easily answered.
I'm going to have to make some assumptions.

Its not clear what you mean by 'objects'.
I'll assume it is a list of arbitrary objects created by your code.

The do_something() function takes an object as an input but does nothing 
with it except pass a property to a constructor and print
it at the end. So there can be no possible impact on your objects list 
from the function in that respect.

The rest of the function creates a new local object, calls a method, 
does some stuff, and then the object gets killed off when you exit
the function. Since this new object is not inserted into your 'objects' 
list (assuming you don't do that inside method() or __init__ or somesuch 
invisible magic) again no harm can befall 'objects'

So the for loop iterates over objects but makes no changes so there
can be no need of threading.

All of that based on the generic code you show and my assumptions above.

Alan G
Author of the Learn to Program web site
Follow my phopto-blog on Flickr at:

More information about the Tutor mailing list