Message278652
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. |
|
Date |
User |
Action |
Args |
2016-10-14 15:01:36 | siming85 | set | recipients:
+ siming85 |
2016-10-14 15:01:36 | siming85 | set | messageid: <1476457296.79.0.655261011887.issue28442@psf.upfronthosting.co.za> |
2016-10-14 15:01:36 | siming85 | link | issue28442 messages |
2016-10-14 15:01:36 | siming85 | create | |
|