python - How to convert version 3.x "yield from" to something compatible in version 2.7? -


this works fine python 3.5 .i understand yield not available in python 2.7. how can implement depth_first() function using python 2.7?

the following solution did not me: converting "yield from" statement python 2.7 code

class node:  def __init__(self, value):     self._value = value     self._children = []   def __repr__(self):     return 'node({!r})'.format(self._value)   def add_child(self, node):     self._children.append(node)   def __iter__(self):     return iter(self._children)   def depth_first(self):     yield self     c in self:         yield c.depth_first()  # example if __name__ == '__main__':     root = node(0)     child1 = node(1)     child2 = node(2)     root.add_child(child1)     root.add_child(child2)     child1.add_child(node(3))     child1.add_child(node(4))     child2.add_child(node(5))     ch in root.depth_first():         print(ch) 

this expected output:

node(0), node(1), node(3), node(4), node(2), node(5) 

convert yield from for-loop plain yield.

convert class node: class node(object): ensure new-style class.

the code works in python 2.7.

class node(object):  def __init__(self, value):     self._value = value     self._children = []   def __repr__(self):     return 'node({!r})'.format(self._value)   def add_child(self, node):     self._children.append(node)   def __iter__(self):     return iter(self._children)   def depth_first(self):     yield self     c in self:         n in c.depth_first():             yield n  # example if __name__ == '__main__':     root = node(0)     child1 = node(1)     child2 = node(2)     root.add_child(child1)     root.add_child(child2)     child1.add_child(node(3))     child1.add_child(node(4))     child2.add_child(node(5))     ch in root.depth_first():         print(ch) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -