Python Forum
what is [o,]? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: what is [o,]? (/thread-1702.html)



what is [o,]? - landlord1984 - Jan-21-2017

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'>



RE: what is [o,]? - buran - Jan-21-2017

short answer - o is argument of gen function. I assume it is short for object...
But I expect you actually want to understand what print_all_2 does, right?

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)
for oNode in gen(self): iterate over all elements yielded from generator gen which takes as argument self.
def gen(o) is generator that yields one Node at a time from a list lAll. lAll = [o,] initialize list to [self,]. Every time before gen yield one node [the one with index 0] it adds node's children to the end of the list. After it yield the first node (i.e. self), the lAll will hold all children of self and so on. In practice print_all_2 will print all nodes, starting from self.

hope this helps


RE: what is [o,]? - landlord1984 - Jan-21-2017

(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?


RE: what is [o,]? - buran - Jan-21-2017

(Jan-21-2017, 07:03 PM)landlord1984 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?

no, comma is not necessary, anyway it is list.
>>> x=[1]
>>> type(x)
<type 'list'>
>>> y=[1,]
>>> type(y)
<type 'list'>



RE: what is [o,]? - ichabod801 - Jan-21-2017

Note that brackets [] define a list, and parentheses () define a tuple. A comma is not needed for a one item list, [o] works fine. But a comma is needed for a one item tuple. (o,) will be read as tuple, but (o) will be read as an expression that simplifies to o.


RE: what is [o,]? - landlord1984 - Jan-22-2017

(Jan-21-2017, 09:15 PM)ichabod801 Wrote: Note that brackets [] define a list, and parentheses () define a tuple. A comma is not needed for a one item list, [o] works fine. But a comma is needed for a one item tuple. (o,) will be read as tuple, but (o) will be read as an expression that simplifies to o.

Thanks. I see. When it is a single element, [o] would be problematic.


RE: what is [o,]? - buran - Jan-22-2017

[o] is fine - that's list, but (o) is problematic.