[issue28442] tuple(a list subclass) does not iterate through the list

Siming Yuan report at bugs.python.org
Fri Oct 14 11:01:36 EDT 2016


New submission from Siming Yuan:

if you subclass a list, and cast it to tuple, the casting does not iterate through the list.
(casing to list does)

for example, i needed a WeakList where the list only internally contains WeakReferences that gets deleted as soon as the object ref count goes to zero.. so:

import weakref

class WeakList(list):

    def __init__(self, items = ()):
        super(WeakList, self).__init__([weakref.ref(i, self.remove) for i in items])

    def __contains__(self, item):
        return super(WeakList, self).__contains__(weakref.ref(item))

    def __getitem__(self, index):
        if isinstance(index, slice):
            return [i() for i in super(WeakList, self).__getitem__(index)]
        else:
            return super(WeakList, self).__getitem__(index)()

    def __setitem__(self, index, item):
        if isinstance(index, slice):
            item = [weakref.ref(i, self.remove) for i in item]
        else:
            item = weakref.ref(item, self.remove)

        return super(WeakList, self).__setitem__(index, item)

    def __iter__(self):
        for i in list(super(WeakList, self).__iter__()):
            yield i()

    def remove(self, item):
        if isinstance(item, weakref.ReferenceType):
            super(WeakList, self).remove(item)
        else:
            super(WeakList, self).remove(weakref.ref(item))

    def append(self, item):
        return super(WeakList, self).append(weakref.ref(item, self.remove))


# write some test code:
class Dummy():
    pass
    
a = Dummy()
b = Dummy()

l = WeakList()
l.append(a)
l.append(b)

print(a)
<__main__.Dummy instance at 0x7f29993f4ab8>

print(b)
<__main__.Dummy instance at 0x7f29993f4b00>

print(l)
[<weakref at 0x7f2999433e68; to 'instance' at 0x7f29993f4ab8>, <weakref at 0x7f2999433ec0; to 'instance' at 0x7f29993f4b00>]

print([i for i in l])
[<__main__.Dummy instance at 0x7f29993f4ab8>, <__main__.Dummy instance at 0x7f29993f4b00>]

print(list(l))
[<__main__.Dummy instance at 0x7f29993f4ab8>, <__main__.Dummy instance at 0x7f29993f4b00>]

print(tuple(l))
(<weakref at 0x7f2999433e68; to 'instance' at 0x7f29993f4ab8>, <weakref at 0x7f2999433ec0; to 'instance' at 0x7f29993f4b00>)

^ notice how you are getting weak references back instead of tuples.

----------
messages: 278652
nosy: siming85
priority: normal
severity: normal
status: open
title: tuple(a list subclass) does not iterate through the list
type: behavior
versions: Python 2.7, Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28442>
_______________________________________


More information about the Python-bugs-list mailing list