为什么是python重用一个类实例里面的函数 [英] why is python reusing a class instance inside in function

查看:126
本文介绍了为什么是python重用一个类实例里面的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个函数中运行一个for循环,该函数创建一个类的实例来测试它们。而不是制作新的类似乎是重复使用相同的两个一遍又一遍。

I'm running a for loop inside a function which is creating instances of a class to test them. instead of making new classes it appears to be reusing the same two over and over.

有什么我缺少关于如何处理类和变量在python方法?

Is there something I'm missing about 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,)

这里是输出

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

当我使用单独的变量名时我得到每个实例的单独的id: p>

when I use seperate variable names I get seperate ids 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)))

输出:

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


推荐答案

对象被重用,而不是新对象没有被实例化。在每次迭代集合被覆盖,因此上一个对象的引用计数下降,Python解释器可以释放其内存并重用它(对于下一个对象)。



All that shows is that the memory of the objects is being reused, not that new objects aren't being instantiated. In each iteration collection is being overwritten, hence the previous object's reference count drops and the Python interpreter is free to deallocate 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

在这种情况下,将重复使用2个内存位置。如果您要将其添加到列表(或将其保存到其他位置),它将保留:

In this case, 2 memory locations are being reused. If you were to add it to a list (or save it elsewhere), it would be preserved:

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

这篇关于为什么是python重用一个类实例里面的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆