multivariable assignment

Discussion in 'Python' started by davidj411, Dec 31, 2009.

  1. davidj411

    davidj411 Guest

    I am not sure why this behavior is this way.
    at beginning of script, i want to create a bunch of empty lists and
    use each one for its own purpose.
    however, updating one list seems to update the others.
    ['1', '1', '1']
     
    davidj411, Dec 31, 2009
    #1
    1. Advertisements

  2. No, you're only creating one list (and giving that one list three
    names). Creating three lists would be:

    a = []
    b = []
    c = []

    or, alternatively

    a, b, c = [], [], []

    Just remember: Python "variables" are just names that point to the
    actual data. "x = y" just means "make 'x' a synonym for 'y'".

    /W
     
    Andreas Waldenburger, Dec 31, 2009
    #2
    1. Advertisements

  3. davidj411

    John Posner Guest

    Revising/expanding Andreas's last paragraph (trying to be correct, but not
    trying to be complete):

    Python deals with NAMEs and OBJECTs. A NAME is "bound" or "assigned" to an
    OBJECT. Each OBJECT can have any number of NAMEs. A NAME cannot be
    assigned to another NAME -- only to an OBJECT.

    Objects are created by EXPRESSIONS, such as:

    42
    42 + 3*othernum
    "spam"
    "spam" + "eggs"
    empname[4:10] <-- slice of a string or other sequence
    cook("spam" + "eggs", 7) <-- calling a function
    "spam".split() <-- calling an object method
    Brunch("spam", veggie, dessert) <-- instantiating a class

    NAMEs are created by assignment statements. You can also reuse an existing
    NAME in a subsequent assignment statement.

    Some assignment statements are of the form:

    NAME = EXPRESSION

    Ex:

    grand_total = 42
    paragraph = " ".join(sentence1, sentence2)
    grand_total = 42 + 3*othernum
    sunday_brunch = Brunch(Eggs(2), "broccoli", "cookie")

    This form:

    1a. Creates a new NAME,
    or
    1b. Removes an existing NAME from the OBJECT to which it is currently
    assigned.
    2. Assigns the NAME to the OBJECT created by the EXPRESSION.

    Other assignment statements are of the form:

    NAME2 = NAME1

    This form does not create a new OBJECT. Instead, it assigns NAME2 as an
    additional name for the object to which NAME1 is currently assigned. Ex:

    y = x
    tenth_item = mylist[9] <-- a list's items have auto-assigned integer
    NAMEs
    clr = colors["sea foam"] <-- a dict has user-devised NAMEs called "keys"
    red_comp = rgb_color.red <-- a class instance has user-devised NAMEs
    called "attributes"

    My favorite metaphor for Python's NAMEs-and-OBJECTs scheme is the
    Post-It(r). NAMEs are like Post-Its; you can stick any number of Post-Its
    to any object.

    HTH (and sorry if the above is overly pedantic),
    John
     
    John Posner, Dec 31, 2009
    #3
  4. davidj411

    Lie Ryan Guest

    Every time people get confused because of how python object model works,
    I always refer them to this article:
    http://effbot.org/zone/call-by-object.htm
     
    Lie Ryan, Jan 1, 2010
    #4
  5. davidj411

    Rainer Grimm Guest

    a, b and c are the same objects.
    You can check the object identity by
    True

    Greetings
     
    Rainer Grimm, Jan 2, 2010
    #5
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.