set and dict iteration
castironpi at gmail.com
Fri Aug 17 20:11:52 CEST 2012
On Thursday, August 16, 2012 9:30:42 PM UTC-5, Paul Rubin wrote:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
> > Luckily, Python is open source. If anyone thinks that sets and dicts
> > should include more code protecting against mutation-during-iteration,
> > they are more than welcome to come up with a patch. Don't forget unit and
> > regression tests, and also a set of timing results which show that the
> > slow-down isn't excessive.
> It could be a debugging option, in which case even a fairly significant
> slowdown is acceptable.
Another possibility is to use the 'gc.get_referrers' mechanism to obtain the iterators.
a= set( ( 0, 1, 2 ) )
b= iter( a )
c= iter( a )
d= iter( a )
print( gc.get_referrers( a ) )
[<set_iterator object at 0x00C0B9E0>, <set_iterator object at 0x00C0BA08>, <set_iterator object at 0x00C0BA30>, [others] ]
This approach wouldn't be as time-efficient as a dedicated secondary structure, due to the other objects which refer to the set, including variable namespaces.
More information about the Python-list