Contents

Lec15-Mutability

Mutability

date

/lec15-mutability/image.png

Obj in Python

/lec15-mutability/image-1.png

String

1
2
s = "Hello"
s.swapcase() # "hELLO"

ASCII

/lec15-mutability/image-3.png 和表格对应 /lec15-mutability/image-2.png 0x41 –> row 4, col 1 😮

1
2
3
4
5
from unicodedata import name, lookup
name('A') # 'LATIN CAPITAL LETTER A'
lookup('LATIN CAPITAL LETTER A') # 'A'
lookup('SNOWMAN') # '☃'
lookup('FACE WITH TEARS OF JOY').encode('utf-8') # '😂'.encode('utf-8')

Mutation operations

Mutable objects

  • List
  • Dictionary
  • Set?
1
2
3
4
5
6
# List, pop, remove, append, extend
lst = [1, 2, 3]
lst.pop() # 3
lst.remove(2) # [1]
lst.append(4) # [1, 4]
lst.extend([5, 6]) # [1, 4, 5, 6]

赋值的时候,如果是可变对象,则会影响到原对象,如果是不可变对象,则会创建新的对象。 😮

Immutable objects

  • Tuple
  • String
  • Number

/lec15-mutability/image-4.png

Immutable objects are hashable, which means that they can be used as keys in dictionaries and as elements in sets.

/lec15-mutability/image-5.png

An immutable object can be changed if it contains a mutable object.

1
2
3
s = ([1, 2], 4)
s[0][0] = 8 # correct
s[0] = 5 # incorrect

Mutation

same or change?

  • Identity: a is b is in Python

  • Equality: a == b == in Python

1
2
3
4
5
6
7
def f(s=[]):
    s.append(1)
    return s

f() # [1]
f() # [1, 1]
f() # [1, 1, 1]

frame里面引用传递更加常见 🤔