Objects, lists and assigning values

Discussion in 'Python' started by Manuel Graune, Apr 5, 2007.

  1. Hello,

    while trying to learn how to program using objects in python (up to now
    simple scripts were sufficient for my needs) I stumbled over the
    a problem while assigning values to an object.

    The following piece of code shows what I intend to do:

    <---snip--->

    class new_class(object):
    def __init__( self,
    internal_list=[]):
    self.internal_list= internal_list

    external_list=[[b*a for b in xrange(1,5)] for a in xrange(1,5)]
    print external_list

    first_collection=[new_class() for i in xrange(4)]

    temporary_list=[[] for i in xrange(4)]
    for i in xrange(4):
    for j in xrange(4):
    temporary_list.append(external_list[j])
    first_collection.internal_list=temporary_list


    #Now everything is as I want it to be:
    for i in xrange(4):
    print first_collection.internal_list


    #Now I tried to get the same result without the temporary
    #variable:

    second_collection=[new_class() for i in xrange(4)]

    for i in xrange(4):
    for j in xrange(4):
    second_collection.internal_list.append(external_list[j])

    #Which obviously leads to a very different result:

    for i in xrange(4):
    print second_collection.internal_list

    <---snip--->

    Can someone explain to me, what's happening here and why the two
    approaches do not lead to the same results? Thanks in Advance.

    Regards,

    Manuel




    --
    A hundred men did the rational thing. The sum of those rational choices was
    called panic. Neal Stephenson -- System of the world
    http://www.graune.org/GnuPG_pubkey.asc
    Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
    Manuel Graune, Apr 5, 2007
    #1
    1. Advertising

  2. En Thu, 05 Apr 2007 14:13:43 -0300, Manuel Graune <>
    escribió:

    > class new_class(object):
    > def __init__( self,
    > internal_list=[]):
    > self.internal_list= internal_list


    All your instances share the *same* internal list, because default
    arguments are evaluated only once (when the function is defined)
    The usual way is to write:

    class new_class(object):
    def __init__(self, internal_list=None):
    if internal_list is None:
    internal_list = []
    self.internal_list= internal_list

    See
    http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm

    --
    Gabriel Genellina
    Gabriel Genellina, Apr 5, 2007
    #2
    1. Advertising

  3. Hello Gabriel, hello William,

    thanks to both of you for your answers. I seem to need a
    better book about python.

    Regards,

    Manuel

    "Gabriel Genellina" <> writes:

    > class new_class(object):
    > def __init__(self, internal_list=None):
    > if internal_list is None:
    > internal_list = []
    > self.internal_list= internal_list
    >
    > See
    > http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm
    >
    > --
    > Gabriel Genellina
    >


    --
    A hundred men did the rational thing. The sum of those rational choices was
    called panic. Neal Stephenson -- System of the world
    http://www.graune.org/GnuPG_pubkey.asc
    Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
    Manuel Graune, Apr 6, 2007
    #3
  4. Manuel Graune

    7stud Guest

    On Apr 6, 1:23 am, Manuel Graune <> wrote:
    > Hello Gabriel, hello William,
    >
    > thanks to both of you for your answers. I seem to need a
    > better book about python.
    >


    What book are you reading?
    7stud, Apr 6, 2007
    #4
  5. "7stud" <> writes:

    >
    > What book are you reading?
    >


    I worked my way through most of the online-docs. A bit to casual
    obviously.

    As printed desktop-reference I use a german book called
    "Python ge-packt".


    --
    A hundred men did the rational thing. The sum of those rational choices was
    called panic. Neal Stephenson -- System of the world
    http://www.graune.org/GnuPG_pubkey.asc
    Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
    Manuel Graune, Apr 7, 2007
    #5
  6. Manuel Graune

    7stud Guest

    On Apr 7, 2:52 pm, Manuel Graune <> wrote:
    > "7stud" <> writes:
    >
    > > What book are you reading?

    >
    > I worked my way through most of the online-docs. A bit to casual
    > obviously.
    >


    See the online tutorial's section on default function arguments here:

    http://docs.python.org/tut/node6.html#SECTION006710000000000000000

    specifically the "Important Warning".
    7stud, Apr 7, 2007
    #6
    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. Marijn
    Replies:
    2
    Views:
    380
    Marijn
    Aug 8, 2003
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    384
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Daniel Nogradi
    Replies:
    3
    Views:
    342
    Dennis Lee Bieber
    Nov 10, 2006
  4. J. Peng
    Replies:
    9
    Views:
    284
    George Sakkis
    Jan 17, 2008
  5. weston
    Replies:
    1
    Views:
    238
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page