Mutables - Non mutables

的 Christian Nguyen, Joseph Razik, last modified 在 2019-10-18
mutables_Non_mutables
In [1]:
import pocketnoobj
Initialisation de l'espace de nommage fait.
In [2]:
def m1(plist):
    plist[0] /= 10
    print("plist = ", plist)
    plist = [10, 11, 12]
    print("plist = ", plist)
In [3]:
ll = [1, 2, 3]
print("ll = ", ll)
ll =  [1, 2, 3]
In [4]:
ret = m1(ll)
plist =  [0.1, 2, 3]
plist =  [10, 11, 12]
In [5]:
print("ll = ", ll)
print("ret = ", ret)
ll =  [0.1, 2, 3]
ret =  None
In [6]:
print(id(ll))
140172763300680
In [1]:
%load_ext tutormagic
Loading extensions from ~/.ipython/extensions is deprecated. We recommend managing extensions like any other Python packages, in site-packages.
In [2]:
%%tutor
def m1(plist):
    plist[0] /= 10
    print("plist = ", plist)
    plist = [10, 11, 12]
    print("plist = ", plist)

ll = [1, 2, 3]
ret = m1(ll)

Opération de concaténation

In [9]:
def m2(plist):
    print(id(plist))
    plist = plist + [4]  # variable local = paramètre + [4]
    print(id(plist))
    return plist
In [10]:
ret = m2(ll)
print("ll = ", ll)
print("ret = ", ret)
140172763300680
140172712816968
ll =  [0.1, 2, 3]
ret =  [0.1, 2, 3, 4]
In [11]:
%%tutor
def m2(plist):
    print(id(plist))
    plist = plist + [4]  # variable local = paramètre + [4]
    print(id(plist))
    return plist
ret = m2(ll)

Opération de concaténation en place

In [12]:
def m3(plist):
    print(id(plist))
    plist += [5]
    print(id(plist))
In [13]:
print(id(ll))
ret = m3(ll)
print("ll = ", ll)
print("ret = ", ret)
140172763300680
140172763300680
140172763300680
ll =  [0.1, 2, 3, 5]
ret =  None
In [14]:
%%tutor
def m3(plist):
    print(id(plist))
    plist += [5]
    print(id(plist))
ret = m3(ll)
In [15]:
def m4(plist):
    print(id(plist))
    append(plist, 6)
    print(id(plist))
In [16]:
print(id(ll))
ret = m4(ll)
print("ll = ", ll)
print("ret = ", ret)
140172763300680
140172763300680
140172763300680
ll =  [0.1, 2, 3, 5, 6]
ret =  None
In [17]:
%%tutor
def m4(plist):
    print(id(plist))
    append(plist, 6)
    print(id(plist))
ret = m4(ll)