Someone enlightened me

Discussion in 'Python' started by Marcus Low, Jul 13, 2008.

  1. Marcus Low

    Marcus Low Guest

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

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

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

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

    for i in globallist :
    print i.lister

    #--------------------------------------------------------------
    test()
    #--------------------------------------------------------------
     
    Marcus Low, Jul 13, 2008
    #1
    1. Advertising

  2. Marcus Low

    bukzor Guest

    On Jul 12, 7:08 pm, Marcus Low <> wrote:
    > Can someone explain to me, why the behaviour below is different when u
    > remark "lister" and unremark "self.lister"?
    >
    > #--------------------------------------------------------------
    > class abc :
    > # remark this later and unremark "self.lister"
    > lister = []
    >
    > def __init__ (self, val):
    > #self.lister = []
    > self.lister.append(val)
    >
    > #--------------------------------------------------------------
    > globallist = []
    > #--------------------------------------------------------------
    > def test () :
    > global l
    > for x in range(10) :
    > o = abc(x)
    > globallist.append(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
     
    bukzor, Jul 13, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toby Considine

    Re: I m gonna mad Plz someone help me

    Toby Considine, Aug 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,263
    Toby Considine
    Aug 31, 2003
  2. Richard Sloan

    Can someone tell me what this is doing

    Richard Sloan, Jan 8, 2006, in forum: Perl
    Replies:
    1
    Views:
    2,160
    Jon Phipps
    May 19, 2006
  3. Ian
    Replies:
    0
    Views:
    1,842
  4. Grocery Clerk

    need to be enlightened on pid_t

    Grocery Clerk, Oct 3, 2004, in forum: C Programming
    Replies:
    4
    Views:
    3,509
    Fredrik Tolf
    Oct 4, 2004
  5. Marcus Low

    Someone enlightened me

    Marcus Low, Jul 13, 2008, in forum: Python
    Replies:
    0
    Views:
    306
    Marcus Low
    Jul 13, 2008
Loading...

Share This Page