Someone enlightened me

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcus Low

    Someone enlightened me

    Can someone explain to me, why the behaviour below is different when u
    remark "lister" and unremark "self.liste r"?

    #--------------------------------------------------------------
    class abc :
    # remark this later and unremark "self.liste r"
    lister = []

    def __init__ (self, val):
    #self.lister = []
    self.lister.app end(val)

    #--------------------------------------------------------------
    globallist = []
    #--------------------------------------------------------------
    def test () :
    global l
    for x in range(10) :
    o = abc(x)
    globallist.appe nd(o)
    o = ""

    for i in globallist :
    print i.lister

    #--------------------------------------------------------------
    test()
    #--------------------------------------------------------------


  • bukzor

    #2
    Re: Someone enlightened me

    On Jul 12, 7:08 pm, Marcus Low <mar...@interne tnowasp.netwrot e:
    Can someone explain to me, why the behaviour below is different when u
    remark "lister" and unremark "self.liste r"?
    >
    #--------------------------------------------------------------
    class abc :
    # remark this later and unremark "self.liste r"
    lister = []
    >
    def __init__ (self, val):
    #self.lister = []
    self.lister.app end(val)
    >
    #--------------------------------------------------------------
    globallist = []
    #--------------------------------------------------------------
    def test () :
    global l
    for x in range(10) :
    o = abc(x)
    globallist.appe nd(o)
    o = ""
    >
    for i in globallist :
    print i.lister
    >
    #--------------------------------------------------------------
    test()
    #--------------------------------------------------------------
    The way it's written, you're appending to a list associated with the
    class itself, which is created only once, then printing out that list
    10 times. After you uncomment and comment the specified lines (this is
    the usual term, rather than "remark"), you are using a list that is
    associated with the actual object, then printing out the 10 different
    lists.

    Hope that's clear enough.
    --Buck

    Comment

    Working...