What's wrong with this code?

Discussion in 'Python' started by Stone Li, Jul 23, 2012.

  1. Stone Li

    Stone Li Guest

    I'm totally confused by this code:

    Code:

    > a = None
    > b = None
    > c = None
    > d = None
    > x = [[a,b],
    > [c,d]]
    > e,f = x[1]
    > print e,f
    > c = 1
    > d = 2
    > print e,f
    > e = 1
    > f = 2
    > print c,d
    >
    > Output:


    None None
    > None None
    > 1 2
    >



    I'm expecting the code as:

    > None None
    > 1 2
    > 1 2
    >
    >


    What's wrong?
    And this question made my GUI program totally out of control.
    Thanks[?]
     
    Stone Li, Jul 23, 2012
    #1
    1. Advertising

  2. On 23/07/2012 15:50, Stone Li wrote:
    > I'm totally confused by this code:
    >
    > Code:
    >
    > a = None
    > b = None
    > c = None
    > d = None
    > x = [[a,b],
    > [c,d]]
    > e,f = x[1]
    > print e,f
    > c = 1
    > d = 2
    > print e,f
    > e = 1
    > f = 2
    > print c,d
    >
    > Output:
    >
    > None None
    > None None
    > 1 2
    >
    >
    >
    > I'm expecting the code as:
    >
    > None None
    > 1 2
    > 1 2
    >
    >
    >
    > What's wrong?
    > And this question made my GUI program totally out of control.
    > Thanks


    c = 1 and d = 2 are overwriting the variable c (= None) and d (= None)
    with new variables 1 and 2. As x already captured c and d while they
    were none, the variables e and f do not change (not would the, even if
    you subsequently changed x)

    Python is a statically scoped language, whereas the functionality you
    are expecting would be an example of dynamically scoped.

    Care to reveal your programming background?

    ~Andrew
     
    Andrew Cooper, Jul 24, 2012
    #2
    1. Advertising

  3. On 24/07/2012 02:19, Andrew Cooper wrote:
    > On 23/07/2012 15:50, Stone Li wrote:
    >> I'm totally confused by this code:
    >>
    >> Code:
    >>
    >> a = None
    >> b = None
    >> c = None
    >> d = None
    >> x = [[a,b],
    >> [c,d]]
    >> e,f = x[1]
    >> print e,f
    >> c = 1
    >> d = 2
    >> print e,f
    >> e = 1
    >> f = 2
    >> print c,d
    >>
    >> Output:
    >>
    >> None None
    >> None None
    >> 1 2
    >>
    >>
    >>
    >> I'm expecting the code as:
    >>
    >> None None
    >> 1 2
    >> 1 2
    >>
    >>
    >>
    >> What's wrong?
    >> And this question made my GUI program totally out of control.
    >> Thanks

    >
    > c = 1 and d = 2 are overwriting the variable c (= None) and d (= None)
    > with new variables 1 and 2. As x already captured c and d while they
    > were none, the variables e and f do not change (not would the, even if
    > you subsequently changed x)
    >
    > Python is a statically scoped language, whereas the functionality you
    > are expecting would be an example of dynamically scoped.
    >
    > Care to reveal your programming background?
    >
    > ~Andrew
    >


    <duck and cover>

    strictly speaking Python doesn't have variables, it has names. This
    will possibly start a flame war which, by the standards of this ng/ml,
    will be an intense conflagration, hence the duck and cover.

    </duck and cover>

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Jul 24, 2012
    #3
  4. On Tue, Jul 24, 2012 at 2:23 AM, Mark Lawrence <> wrote:
    > strictly speaking Python doesn't have variables, it has names. This will
    > possibly start a flame war which, by the standards of this ng/ml, will be an
    > intense conflagration, hence the duck and cover.


    The two terms are nearly synonymous when one talks about Python (and
    both are used in the language reference).

    I mean, what's so "strict" about the way you're speaking?

    -- Devin
     
    Devin Jeanpierre, Jul 24, 2012
    #4
  5. There is one model that has helped me much understanding how Python
    ticks and that is the model of name tags. The code "a = 1" creates an
    integer with value 1 and attaches a tag with "a" written on it using a
    small piece of rope. Now, if you attach the tag to a different item, it
    obviously doesn't change the original integer. Also, you can attach more
    than one tag to the integer or even none. Further, a tag doesn't even
    have to be attached to anything (this happens if you use a local
    variable before assigning to it). This operation of tagging something is
    done with the "=" operator.

    Now, coming back to your code...

    Am 23.07.2012 16:50, schrieb Stone Li:
    > I'm totally confused by this code:
    >
    > Code:
    >
    >> a = None
    >> b = None
    >> c = None
    >> d = None


    This adds the tags "a", "b", "c" and "d" to None.


    >> x = [[a,b],
    >> [c,d]]


    "[a, b]" creates a list, containing two anonymous tags (they don't have
    anything written on them but they are accessible via index) attached to
    what "a" and "b" are currently attached to [0]. The same happens for
    "[c, d]". The two lists are then put into another list with a similar
    mechanism, and that list of lists is then tagged "x".


    >> e,f = x[1]


    This takes the second element of "x" (the [c, d] above) and tags it with
    "e" and "f". This syntax implicitly unpacks the list so the assignment
    operator adds the two tags "e" and "f" to the first and second element
    referenced by that list. Both "e" and "f" finally end up attached to "None".


    >> c = 1
    >> d = 2


    These two remove the rope attaching "c" and "d" to "None" and instead
    attach them to the integers "1" and "2".


    I hope your Python's behaviour makes sense to you now!

    Uli


    [0] Note that in almost all cases, when referring to a tag, Python
    implicitly operates on the object attached to it. One case (the only
    one?) where it doesn't is the "del" statement.
     
    Ulrich Eckhardt, Jul 24, 2012
    #5
  6. On Tue, Jul 24, 2012 at 5:47 PM, Ulrich Eckhardt
    <> wrote:
    > There is one model that has helped me much understanding how Python ticks
    > and that is the model of name tags. The code "a = 1" creates an integer with
    > value 1 and attaches a tag with "a" written on it using a small piece of
    > rope.


    A double strand of rope, I think. If it were one strand, we'd write "a - 1". :)

    > [0] Note that in almost all cases, when referring to a tag, Python
    > implicitly operates on the object attached to it. One case (the only one?)
    > where it doesn't is the "del" statement.


    I'd say that del is more closely related to assignment than anything
    else. You can go "a = None" to mark that a now points to nothing, or
    you can "del a" to de-rope a altogether.

    ChrisA
     
    Chris Angelico, Jul 24, 2012
    #6
  7. Stone Li

    Ian Kelly Guest

    On Mon, Jul 23, 2012 at 7:19 PM, Andrew Cooper <> wrote:
    > Python is a statically scoped language, whereas the functionality you
    > are expecting would be an example of dynamically scoped.


    While it's true that Python is statically scoped, that has nothing at
    all to do with the code from the OP's question, which contains only
    one (global) scope anyway.

    The real issue is confusion between name binding and object mutation.
    By reassigning c and d, the OP is evidently trying to mutate the list
    named x (or to be more precise, the list that is the second element of
    the list named x). But this doesn't work, because name-binding
    doesn't mutate objects as it does in languages with variable semantics
    (C, for example); it merely rebinds the names to different objects. c
    and d end up bound to values that weren't in the list to begin with,
    meanwhile the list remains unchanged and e and f are still just bound
    to None.
     
    Ian Kelly, Jul 24, 2012
    #7
  8. On Tue, 24 Jul 2012 09:47:38 +0200, Ulrich Eckhardt wrote:

    > [0] Note that in almost all cases, when referring to a tag, Python
    > implicitly operates on the object attached to it. One case (the only
    > one?) where it doesn't is the "del" statement.


    Name-binding:

    x = 1

    operates on the name "x", not the object. The object 1 does not know it
    has been bound to anything -- that's one weakness of the "tag" model,
    because it implies that objects know what tags they have attached. They
    don't.

    Imports:

    import x

    also operates on the name x and not the object.


    --
    Steven
     
    Steven D'Aprano, Jul 24, 2012
    #8
  9. Am 23.07.2012 16:50 schrieb Stone Li:
    > I'm totally confused by this code:
    >
    > Code:
    >
    > a = None
    > b = None
    > c = None
    > d = None
    > x = [[a,b],
    > [c,d]]
    > e,f = x[1]
    > print e,f
    > c = 1
    > d = 2
    > print e,f
    > e = 1
    > f = 2
    > print c,d
    >
    > Output:
    >
    > None None
    > None None
    > 1 2
    >
    >
    > I'm expecting the code as:
    >
    > None None
    > 1 2
    > 1 2
    >
    >
    > What's wrong?


    Your expectation :)

    With c = 1 and d = 2 you do not change the respective objects, but you
    assign other objects to the same names.

    The old content is still contained in x[1].

    If you would only modify these objects (not possible as ints are
    immutable), you would notice the changes here and there.


    Thomas
     
    Thomas Rachel, Jul 24, 2012
    #9
  10. Am 24.07.2012 09:47 schrieb Ulrich Eckhardt:

    > [0] Note that in almost all cases, when referring to a tag, Python
    > implicitly operates on the object attached to it. One case (the only
    > one?) where it doesn't is the "del" statement.


    The del and the =, concerning the left side.

    But even those don't do that under all circumstances. Think about
    __setitem__, __setattr__, __set__, __delitem__, __delattr__, __delete__.


    Thomas
     
    Thomas Rachel, Jul 24, 2012
    #10
  11. Am 24.07.2012 10:24, schrieb Chris Angelico:
    > On Tue, Jul 24, 2012 at 5:47 PM, Ulrich Eckhardt
    > <> wrote:
    >> There is one model that has helped me much understanding how Python ticks
    >> and that is the model of name tags. The code "a = 1" creates an integer with
    >> value 1 and attaches a tag with "a" written on it using a small piece of
    >> rope.

    >
    > A double strand of rope, I think. If it were one strand, we'd write "a - 1". :)


    Two fibers, possibly twisted.... let's call it string!

    I really had to think about how to call the thing while avoiding the
    term string in order not to add to the confusion...

    :)

    Uli
     
    Ulrich Eckhardt, Jul 24, 2012
    #11
    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. walala
    Replies:
    3
    Views:
    2,254
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,340
  3. Matthew
    Replies:
    7
    Views:
    811
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    499
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,202
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page