[Python-ideas] Introduce collections.Reiterable

Chris Angelico rosuav at gmail.com
Thu Sep 19 13:52:02 CEST 2013

On Thu, Sep 19, 2013 at 8:39 PM, Neil Girdhar <mistersheik at gmail.com> wrote:
> That was just for illustration.  Here's the code I just fixed to use
> Reiterable:
> class Network:
>     def update(self, nodes):
>         nodes = Reiterable(nodes)
>         super().update(self, nodes)
>         for node in nodes:
>             node.setParent(self)
>             node.propertyValuesChanged.connect(self.modelPropertiesChanged)
>         self.modelNodesAddedRemoved.emit()

Hmm. As an alternative to reiterable, can you rejig the design
something like this?

class Network(...):
    def update(self,nodes):
        for node in nodes: self._update(node)
    def _update(self,node):

You put update() into the highest appropriate place in the class
hierarchy, and then each subclass simply overrides _update to do the
work. That way, you iterate over nodes exactly once, and every point
in the hierarchy gets to do its own _update.


More information about the Python-ideas mailing list