What makes an iterator an iterator?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Thu Apr 19 03:04:56 EDT 2007


On Wed, 18 Apr 2007 01:45:10 -0700, Paul McGuire wrote:

>> For the record, this is what I actually wanted: a four-line self-sorting
>> dictionary:
>>
>> class SortedDict(dict):
>>     def __iter__(self):
>>         for key in sorted(self.keys()):
>>             yield key

[snip]

> Very neat.  Why not this?
> 
> class SortedDict(dict):
>     def __iter__(self):
>         return iter(sorted(self.keys()))


Good question. I don't have a good answer except for "because I didn't
think of it".


-- 
Steven.




More information about the Python-list mailing list