This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: weaklist
Type: enhancement Stage:
Components: None Versions: Python 2.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: fdrake Nosy List: atuells, facundobatista, fdrake, g-lite, georg.brandl, loewis, rhettinger, tim.peters
Priority: low Keywords: patch

Created on 2001-12-01 01:36 by atuells, last changed 2022-04-10 16:04 by admin. This issue is now closed.

File name Uploaded Description Edit atuells, 2001-12-01 01:36
Messages (12)
msg53355 - (view) Author: Andres Tuells (atuells) Date: 2001-12-01 01:36
WeakList are list whose entries are referenced weakly. 
When the object is gc it is deleted from the weaklist 
(and from its iterators). To be added to
msg53356 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2001-12-02 01:07
Logged In: YES 

Thanks for the patch. I would recommend to publish it as a
separate package first, to get user feedback. I can't see
this as a universally-useful data type, so I'm not sure it
should be added to the standard library.

*If* it is added, a number of corrections must be made to
the code:
- remove removes elements by equality, not identity. I
believe in removeAll, you are looking for identical objects,
not merely equal ones.

- Why is it the right thing to remove elements from the list
if the underlying object dies? doesn't this have undesirable
side effects on indexing, e.g. when the list is being
iterated over?

- In the standard library, I think inheriting from UserList
is deprecated, in favour of inheriting from list.

- It seems that the class creates many unnecessary copies of
lists, e.g. in extend, or setslice.

- The references create cycles involving WeakList. Since the
WeakList refers to ref objects through data, and the ref
objects refer to the list throught the callback, the list
itself will become garbage as long as list elements remain
alive (although GC will detect those cycles). That should be

- What is the point of the infinite loop in __getitem__?
msg53357 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2001-12-05 05:31
Logged In: YES 

Needs motivation.  Without an need for the data structure,
this will be rejected.  Lowering priority and marking this
for consideration for Python 2.3; it's too late to add this
for Python 2.2.

Set to "pending" while awaiting an explanation of the
msg53358 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2001-12-05 05:35
Logged In: YES 

Oops, I meant to adjust the priority on this.
msg53359 - (view) Author: S. Kochen (g-lite) Date: 2005-07-26 23:16
Logged In: YES 

Mind if I bring this back up? This doesn't seem to be in
yet. I didn't look at the implementation, but as for

Andres mentioned his original motivation on a list:

I've also seen it duplicated in this recipe:

I'd like to see it in for the same reason.
msg53360 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2005-07-27 01:19
Logged In: YES 

This might be interesting to have.  Would this be more
useful than, say, a weak set?  Ordering may be important for
the publish/subscribe use case, at least to ensure

I've not looked at the contributed code, so can't make any
comment on that.
msg53361 - (view) Author: S. Kochen (g-lite) Date: 2005-07-27 11:51
Logged In: YES 

I'm not sure if either is more useful than the other, they
both seem to have their advantages.

It looks like a set would work for the links I mentioned,
but I'd personally like to have the ability to connect to a
signal/event with priority, thus needing a list.

A weak set could possibly be implemented on top of
WeakKeyDictionary? Have weak sets already been implemented
msg53362 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-07-27 14:02
Logged In: YES 

Weaksets would not be warranted unless the use cases
demonstrated needs for methods unique to sets (intersection,
union, etc).  Otherwise,  weakdictionaries would suffice and
we could avoid bloating the module.

Also, I'm not sure weaklists are a good idea.  First, it is
not clear that the subscription use case can be reliably
implemented with gc as the primary means of unsubscribing --
traditionally, that is done with an explicit unsubscribe()
call issued by the subscriber.

Second, weaklists only have a differential advantage when it
comes to maintaining insertion order.  It is not clear that
that feature is really useful.  What is clear is that the
approach incurs extra costs  for maintaing order and O(n)
removal time.

When order tracking is necessary, there are reasonable
implementations using weakkeydictionaries with the entry
values being a sequence number indicating creation order. 
With that data structure, group notification is still simple:

for subscriber in sorted(wkd, key=wkd.__getitem__):
    self.notify(subscriber, message)

This approach incurs an O(n log n) sort cost for each group
notify but has only an O(1) deletion cost which is an
improvement over weaklists.  The only way I see around the
deletion time issue is to have a weakdoublylinked list which
would allow O(1) deletions, appends, and O(n) iteration.  

None of this came up on the referenced newsgroup posting
because there was NO active discussion.  IOW, the idea has
not shown any demand and has not been through the basic due
diligence needed to tease out the best approach. I recommend
leaving this as a recipe until we see a battlehardened
implementation, a convincing use case, and some user demand.
msg53363 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2005-07-27 18:00
Logged In: YES 

FYI, ZODB has a WeakSet implementation, in here:

I wouldn't add this to the core -- it's very specific to ZODB's 
needs.  For example, it actually uses a weak value 
dictionary, because ZODB can't assume the set elements 
are usably comparable or hashable.

One thing that "should be" addressed in the core is explained 
in a long comment there:  the core weak containers don't 
supply a sane way to iterate over their weakly referenced 
containees.  You either risk unpredictable "dict changed size 
during iteration" exceptions, or unboundedly worse gc 
behavior (read the comment for more detail).  The ZODB 
WeakSet implementation has to break into the weak-value 
dict implementation to supply a partially sane way to iterate 
over its elements ("partially" means it's not incremental, and 
exposes weakrefs to the client; OTOH, it doesn't suffer 
mystery "dict changed size" exceptions, and plays nicely 
with gc).
msg60138 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-01-19 09:57
No news after two years and a half. Considering the arguments of Raymond
after S. Kochen brought the issue back up, I'd close the patch as rejected.

Feel free to bring this issue to python-dev, and if there's real need,
we can always open the patch back.

Fred, it's assigned to you... what do you think?

Thank you!
msg60245 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-19 23:01
Also we already have a WeakSet now since the abc module needs it.
msg61505 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2008-01-22 14:08
Facundo: Agreed as well; since the use case isn't strong, let's avoid
adding this.
Date User Action Args
2022-04-10 16:04:42adminsetgithub: 35642
2008-01-22 14:08:32fdrakesetstatus: open -> closed
resolution: rejected
messages: + msg61505
2008-01-19 23:01:54georg.brandlsetnosy: + georg.brandl
messages: + msg60245
2008-01-19 09:57:30facundobatistasetnosy: + facundobatista
messages: + msg60138
2008-01-05 19:29:05christian.heimessetkeywords: + patch
versions: + Python 2.6
2001-12-01 01:36:06atuellscreate