Why python reuses an instance of a class inside a function

I run a for loop inside a function that instantiates a class to test them. instead of creating new classes, it seems like they reuse the same two over and over again.

Is there something I am missing in how classes and variables are handled in python methods?

how can i generate a new object for each iteration of the loop

class CollectionSetImages(unittest.TestCase): def test_keywordset(self): """Testing keyword queries by images equality """ for keyword in ['a','b','c','d','e','f','g']: images_by_keyword = Image.keyword_query([keyword]) collection = Collection([keyword]) class_images = collection.images print('colleciton: %s id: %s' % (collection,id(collection))) self.assertEqual(images_by_keyword, class_images,) 

displayed here

 colleciton: <tests.fakeimages._FakeCollection object at 0xb7c656cc> id: 3083228876 colleciton: <tests.fakeimages._FakeCollection object at 0xb7c656ec> id: 3083228908 colleciton: <tests.fakeimages._FakeCollection object at 0xb7c656cc> id: 3083228876 colleciton: <tests.fakeimages._FakeCollection object at 0xb7c656ec> id: 3083228908 colleciton: <tests.fakeimages._FakeCollection object at 0xb7c656cc> id: 3083228876 colleciton: <tests.fakeimages._FakeCollection object at 0xb7c656ec> id: 3083228908 colleciton: <tests.fakeimages._FakeCollection object at 0xb7c656cc> id: 3083228876 

when I use separate variable names, I get separate identifiers for each instance, as expected:

 collectionA = Collection(['a']) print('collection: %s id: %s' % (collectionA,id(collectionA))) collectionB = Collection(['f']) print('collection: %s id: %s' % (collectionB,id(collectionB))) collectionC = Collection(['f']) print('collection: %s id: %s' % (collectionC,id(collectionC))) 

outputs:

 collection: <tests.fakeimages._FakeCollection object at 0xb7cbc8ac> id: 3083585708 collection: <tests.fakeimages._FakeCollection object at 0xb7cbccec> id: 3083586796 collection: <tests.fakeimages._FakeCollection object at 0xb7cbcd2c> id: 3083586860 
+6
python namespaces class
source share
2 answers

All that is shown is that the memory of objects is reused, and not that new objects are not created. At each iteration, collection is overwritten, so the previous object reference count drops and the Python interpreter can free its memory and reuse it (for the next object).

 >>> for a in range(1,5): ... b = object() ... print b, id(b) ... <object object at 0xb7db9470> 3084620912 <object object at 0xb7db9468> 3084620904 <object object at 0xb7db9470> 3084620912 <object object at 0xb7db9468> 3084620904 <object object at 0xb7db9470> 3084620912 

In this case, 2 memory cells are reused. If you want to add it to the list (or save it elsewhere), it will be saved:

 >>> a = [] >>> for b in range(1,5): ... c = object() ... a.append(c) ... print c, id(c) ... <object object at 0xb7db9470> 3084620912 <object object at 0xb7db9468> 3084620904 <object object at 0xb7db9478> 3084620920 <object object at 0xb7db9480> 3084620928 
+11
source share

from python documentation:

Identifier () Return the "identifier" of the object. This is an integer (or a long integer) that is guaranteed to be unique and constant for this object throughout its life. Two objects with non-overlapping lifetimes can have the same id () value.

+3
source share

All Articles