Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么python在函数中重用类实例_Python_Namespaces_Class - Fatal编程技术网

为什么python在函数中重用类实例

为什么python在函数中重用类实例,python,namespaces,class,Python,Namespaces,Class,我在一个函数中运行for循环,该函数创建一个类的实例来测试它们。它似乎不是在创建新类,而是一次又一次地重用相同的两个类 关于python方法中如何处理类和变量,我是否缺少一些东西 如何为循环的每次迭代生成新对象 class CollectionSetImages(unittest.TestCase): def test_keywordset(self): """Testing keyword queries by images equality """

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

关于python方法中如何处理类和变量,我是否缺少一些东西

如何为循环的每次迭代生成新对象

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
产出:

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
collection:id:3083585708
收藏:id:3083586796
收藏:id:3083586860

所有这些都表明对象的内存正在被重用,而不是新对象没有被实例化。在每次迭代中,
集合
都被覆盖,因此前一个对象的引用计数会下降,Python解释器可以自由地释放内存并重用它(用于下一个对象)

>>适用于范围内的(1,5):
...     b=对象()
...     打印b,id(b)
... 
3084620912
3084620904
3084620912
3084620904
3084620912
在这种情况下,将重用2个内存位置。如果要将其添加到列表中(或保存到其他位置),则会保留:

>>> 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
>a=[]
>>>对于范围(1,5)内的b:
...     c=对象()
...     a、 附加(c)
...     打印c,id(c)
... 
3084620912
3084620904
3084620920
3084620928

来自python文档:

id()
返回对象的“标识”。这是一个整数(或长整数),保证该对象在其生存期内唯一且不变。生命周期不重叠的两个对象可能具有相同的id()值。

看起来您在其中有一个测试模拟对象。。。您可能希望再次检查模拟类的实例化如何在测试用例中发挥作用。@Jarret,很好,很快就会发布测试对象的输出
>>> 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
>>> 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