Multiple assignments in python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite
1












I need a clear explanation here.
Why does the following code work ?



foo1 = foo1[0] = [0]


Ok, I know assignments are done left to right.



How does python understand foo1 is a list?



Btw I know foo1 ends up as [[...]] its first element being itself.










share|improve this question



















  • 2




    Depends on what foo1 was originally. If looks like it was a list with at least one element in it.
    – Makoto
    55 mins ago






  • 2




    Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of foo1.
    – norok2
    52 mins ago











  • @norok2: Yeah it does. You can't reference foo1[0] if foo1 isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
    – Makoto
    47 mins ago










  • Theoretical question.
    – Jeremie
    46 mins ago






  • 1




    @Makoto Please copy paste OP's code into a fresh interpreter session.
    – timgeb
    29 mins ago














up vote
8
down vote

favorite
1












I need a clear explanation here.
Why does the following code work ?



foo1 = foo1[0] = [0]


Ok, I know assignments are done left to right.



How does python understand foo1 is a list?



Btw I know foo1 ends up as [[...]] its first element being itself.










share|improve this question



















  • 2




    Depends on what foo1 was originally. If looks like it was a list with at least one element in it.
    – Makoto
    55 mins ago






  • 2




    Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of foo1.
    – norok2
    52 mins ago











  • @norok2: Yeah it does. You can't reference foo1[0] if foo1 isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
    – Makoto
    47 mins ago










  • Theoretical question.
    – Jeremie
    46 mins ago






  • 1




    @Makoto Please copy paste OP's code into a fresh interpreter session.
    – timgeb
    29 mins ago












up vote
8
down vote

favorite
1









up vote
8
down vote

favorite
1






1





I need a clear explanation here.
Why does the following code work ?



foo1 = foo1[0] = [0]


Ok, I know assignments are done left to right.



How does python understand foo1 is a list?



Btw I know foo1 ends up as [[...]] its first element being itself.










share|improve this question















I need a clear explanation here.
Why does the following code work ?



foo1 = foo1[0] = [0]


Ok, I know assignments are done left to right.



How does python understand foo1 is a list?



Btw I know foo1 ends up as [[...]] its first element being itself.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 36 mins ago









timgeb

37k104875




37k104875










asked 59 mins ago









Jeremie

643




643







  • 2




    Depends on what foo1 was originally. If looks like it was a list with at least one element in it.
    – Makoto
    55 mins ago






  • 2




    Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of foo1.
    – norok2
    52 mins ago











  • @norok2: Yeah it does. You can't reference foo1[0] if foo1 isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
    – Makoto
    47 mins ago










  • Theoretical question.
    – Jeremie
    46 mins ago






  • 1




    @Makoto Please copy paste OP's code into a fresh interpreter session.
    – timgeb
    29 mins ago












  • 2




    Depends on what foo1 was originally. If looks like it was a list with at least one element in it.
    – Makoto
    55 mins ago






  • 2




    Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of foo1.
    – norok2
    52 mins ago











  • @norok2: Yeah it does. You can't reference foo1[0] if foo1 isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
    – Makoto
    47 mins ago










  • Theoretical question.
    – Jeremie
    46 mins ago






  • 1




    @Makoto Please copy paste OP's code into a fresh interpreter session.
    – timgeb
    29 mins ago







2




2




Depends on what foo1 was originally. If looks like it was a list with at least one element in it.
– Makoto
55 mins ago




Depends on what foo1 was originally. If looks like it was a list with at least one element in it.
– Makoto
55 mins ago




2




2




Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of foo1.
– norok2
52 mins ago





Is the question purely theoretical, or you found any use to this kind of construct? @Makoto it does not depend on the previous use / definition of foo1.
– norok2
52 mins ago













@norok2: Yeah it does. You can't reference foo1[0] if foo1 isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
– Makoto
47 mins ago




@norok2: Yeah it does. You can't reference foo1[0] if foo1 isn't indexable (e.g. like an object which doesn't have that property overridden or a number).
– Makoto
47 mins ago












Theoretical question.
– Jeremie
46 mins ago




Theoretical question.
– Jeremie
46 mins ago




1




1




@Makoto Please copy paste OP's code into a fresh interpreter session.
– timgeb
29 mins ago




@Makoto Please copy paste OP's code into a fresh interpreter session.
– timgeb
29 mins ago












2 Answers
2






active

oldest

votes

















up vote
8
down vote













Because



foo1 = foo1[0] = [0]


is equivalent to



temp = [0]
foo1 = temp
foo1[0] = temp


it first evaluates expression and then assigns from left to right.




assigns the single resulting object to each of the target lists, from left to right
from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements




Update: also see answers to similar question here: Multiple assignment and evaluation order in Python






share|improve this answer





























    up vote
    0
    down vote













    Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0] and it fins a value at the end, which is [0]. It is a list with one element 0. Now, this gets assigned to the first element of the list foo1 through foo1[0] = [0]. But since foo1 is already declared, it creates an object which has a pointer to itself, and hence foo1 gets self-referenced infinitely, with the innermost list having 0.



    The structure of the list foo1 will be the same when the code is foo1 = foo1[0].



    The object foo1 has entered an infinite self-referenced loop.






    share|improve this answer




















      Your Answer





      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52467743%2fmultiple-assignments-in-python%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      8
      down vote













      Because



      foo1 = foo1[0] = [0]


      is equivalent to



      temp = [0]
      foo1 = temp
      foo1[0] = temp


      it first evaluates expression and then assigns from left to right.




      assigns the single resulting object to each of the target lists, from left to right
      from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements




      Update: also see answers to similar question here: Multiple assignment and evaluation order in Python






      share|improve this answer


























        up vote
        8
        down vote













        Because



        foo1 = foo1[0] = [0]


        is equivalent to



        temp = [0]
        foo1 = temp
        foo1[0] = temp


        it first evaluates expression and then assigns from left to right.




        assigns the single resulting object to each of the target lists, from left to right
        from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements




        Update: also see answers to similar question here: Multiple assignment and evaluation order in Python






        share|improve this answer
























          up vote
          8
          down vote










          up vote
          8
          down vote









          Because



          foo1 = foo1[0] = [0]


          is equivalent to



          temp = [0]
          foo1 = temp
          foo1[0] = temp


          it first evaluates expression and then assigns from left to right.




          assigns the single resulting object to each of the target lists, from left to right
          from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements




          Update: also see answers to similar question here: Multiple assignment and evaluation order in Python






          share|improve this answer














          Because



          foo1 = foo1[0] = [0]


          is equivalent to



          temp = [0]
          foo1 = temp
          foo1[0] = temp


          it first evaluates expression and then assigns from left to right.




          assigns the single resulting object to each of the target lists, from left to right
          from here: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements




          Update: also see answers to similar question here: Multiple assignment and evaluation order in Python







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 35 mins ago

























          answered 40 mins ago









          Eduard

          35816




          35816






















              up vote
              0
              down vote













              Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0] and it fins a value at the end, which is [0]. It is a list with one element 0. Now, this gets assigned to the first element of the list foo1 through foo1[0] = [0]. But since foo1 is already declared, it creates an object which has a pointer to itself, and hence foo1 gets self-referenced infinitely, with the innermost list having 0.



              The structure of the list foo1 will be the same when the code is foo1 = foo1[0].



              The object foo1 has entered an infinite self-referenced loop.






              share|improve this answer
























                up vote
                0
                down vote













                Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0] and it fins a value at the end, which is [0]. It is a list with one element 0. Now, this gets assigned to the first element of the list foo1 through foo1[0] = [0]. But since foo1 is already declared, it creates an object which has a pointer to itself, and hence foo1 gets self-referenced infinitely, with the innermost list having 0.



                The structure of the list foo1 will be the same when the code is foo1 = foo1[0].



                The object foo1 has entered an infinite self-referenced loop.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0] and it fins a value at the end, which is [0]. It is a list with one element 0. Now, this gets assigned to the first element of the list foo1 through foo1[0] = [0]. But since foo1 is already declared, it creates an object which has a pointer to itself, and hence foo1 gets self-referenced infinitely, with the innermost list having 0.



                  The structure of the list foo1 will be the same when the code is foo1 = foo1[0].



                  The object foo1 has entered an infinite self-referenced loop.






                  share|improve this answer












                  Python variables know their types based on the type of variable assigned to it. It is a dynamically types language. In your code, the interpreter sees foo1 = foo1[0] = [0] and it fins a value at the end, which is [0]. It is a list with one element 0. Now, this gets assigned to the first element of the list foo1 through foo1[0] = [0]. But since foo1 is already declared, it creates an object which has a pointer to itself, and hence foo1 gets self-referenced infinitely, with the innermost list having 0.



                  The structure of the list foo1 will be the same when the code is foo1 = foo1[0].



                  The object foo1 has entered an infinite self-referenced loop.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 15 mins ago









                  a_ran

                  387




                  387



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52467743%2fmultiple-assignments-in-python%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery