I used list, def. why li += [100, 200] , and li = li + [100, 200] is different

Rhodri James rhodri at kynesim.co.uk
Mon Oct 23 13:27:28 EDT 2017


On 23/10/17 17:29, 임현준 wrote:
> I am a Korean student, and I am a beginner in English and Python.;(
> 
> I can't understand about this def
> 
> If I want to print
> 
> [1,2,3,4,5]
> [1,2,3,4,5,100,200]
> 
> I will make code like this, and I can understand code.
> 
> 
> def modify(li):
>       li += [100,200]
> > list = [1,2,3,4,5]
> print(list)
> modify(list)
> print(list) 

Here is approximately what Python does when you run this:

* Create a list [1,2,3,4,5]

* Give the list the name "list".  In talking about Python we often say 
that it *binds* the name "list" to the list [1,2,3,4,5].  (It is usually 
a bad idea to use names like "list" that also have a meaning in Python, 
but we will ignore that for now.)


     list ----\
               +-----------+
               | 1,2,3,4,5 |
               +-----------+

* Print the object bound to the name "list", which is the list [1,2,3,4,5].

* Call modify().  This also binds the name "li" to the object that 
"list" is bound to.


     list ----\
               +-----------+
               | 1,2,3,4,5 |
               +-----------+
     li ------/

* Create a new list [100,200]

* "li += [100,200]" for a list means "change the list bound to the name 
'li' by joining the new list onto the end of it.  It is still the same 
object, it just has some more added to it.

     list ----\
               +-------------------+
               | 1,2,3,4,5,100,200 |
               +-------------------+
     li ------/

"list" is still bound to the same object, so you see the changes outside 
the function modify().

> 
> BUT, when I make code like this.
> 
> I will make code like this, and I can understand code.
> 
> 
> def modify(li):
>       li = li + [100,200]

What happens here is a little different.  It starts off the same with 
"li" and "list" bound to the same object:

     list ----\
               +-----------+
               | 1,2,3,4,5 |
               +-----------+
     li ------/

and a new object [100,200] that doesn't have a name.  What happens next is:

* Take the object bound to the name "li" and the unnamed object 
[100,200], and create a *new* object by joining them together.

* Bind the name "li" to this new object

     list ----\
               +-----------+
               | 1,2,3,4,5 |
               +-----------+

     li ------\
               +-------------------+
               | 1,2,3,4,5,100,200 |
               +-------------------+

Notice that it is only the name "li" that is bound.  The name "list" 
still refers to the original object.  When we return from the function, 
we stop using the name "li" and nothing refers to our new object 
[1,2,3,4,5,100,200] any more.  Python will quietly delete it ("garbage 
collect") in the background.

The key is that "+" on its own creates a new object, while "+=" alters 
the existing object.

-- 
Rhodri James *-* Kynesim Ltd



More information about the Python-list mailing list