Jan-21-2017, 07:03 PM
(This post was last modified: Jan-21-2017, 07:03 PM by landlord1984.)
(Jan-21-2017, 08:48 AM)landlord1984 Wrote: I am studying following teaching me about composition of class objects. What is o doing here?def print_all_2(self): def gen(o): lAll = [o,]from following codes:class Node(object): def __init__(self,sName): self._lChildren = [] self.sName = sName def __repr__(self): return "<Node '{}'>".format(self.sName) def append(self,*args,**kwargs): self._lChildren.append(*args,**kwargs) def print_all_1(self): print(self) for oChild in self._lChildren: oChild.print_all_1() def print_all_2(self): def gen(o): lAll = [o,] while lAll: oNext = lAll.pop(0) lAll.extend(oNext._lChildren) yield oNext for oNode in gen(self): print(oNode) oRoot = Node("root") oChild1 = Node("child1") oChild2 = Node("child2") oChild3 = Node("child3") oChild4 = Node("child4") oChild5 = Node("child5") oChild6 = Node("child6") oChild7 = Node("child7") oChild8 = Node("child8") oChild9 = Node("child9") oChild10 = Node("child10") oRoot.append(oChild1) oRoot.append(oChild2) oRoot.append(oChild3) oChild1.append(oChild4) oChild1.append(oChild5) oChild2.append(oChild6) oChild4.append(oChild7) oChild3.append(oChild8) oChild3.append(oChild9) oChild6.append(oChild10) # specify output from here onwards oRoot.print_all_1() oRoot.print_all_2()Quote: <Node 'root'> <Node 'child1'> <Node 'child4'> <Node 'child7'> <Node 'child5'> <Node 'child2'> <Node 'child6'> <Node 'child10'> <Node 'child3'> <Node 'child8'> <Node 'child9'> <Node 'root'> <Node 'child1'> <Node 'child2'> <Node 'child3'> <Node 'child4'> <Node 'child5'> <Node 'child6'> <Node 'child8'> <Node 'child9'> <Node 'child7'> <Node 'child10'>
I was curious and am still curious about [o,]. I have deleted the comma, made it as [o] and run the program again, it still works same. Is the comma necessary?
(Jan-21-2017, 09:28 AM)buran Wrote: I was curious and am still curious about [o,]. I have deleted the comma, made it as [o] and run the program again, it still works same. Is the comma necessary?I was curious and am still curious about [o,]. I have deleted the comma, made it as [o] and run the program again, it still works same. Is the comma necessary?