Michael Sorich wrote:
> Am I correct in assuming the code below is the
> standard way of iterating over components?
>
> for component in self.components.itervalues():
> do stuff
>
> The following seems more intuitive to me.
>
> for component in self.components:
> do stuff
>
> However, this causes a KeyError exception because
> __iter__ is not defined. Is it ambiguous as to what
> self.components should iterate over? If this makes
> sense, an option would be to alias __iter__ in
> WidgetDict (model.py) to UserDict.UserDict.itervalues.
>
>
> Michael
>
Michael,
You are correct. Because the components are an instance of WidgetDict
from model.py and this inherits from UserDict.UserDict there is no
__iter__ method. I'd say that the standard way of iterating over your
components would be;
for component in self.components.keys():
do stuff
To make the WidgetDict more like a standard dictionary the __iter__
method should return the key values and maybe we should look at
implementing this. Does anyone have strong opinions either way?
Regards,
Andy
--
--------------------------------------------------------------------------------
From the desk of Andrew J Todd esq - http://www.halfcooked.com/
|