-
Notifications
You must be signed in to change notification settings - Fork 676
Expand file tree
/
Copy pathcode.py
More file actions
68 lines (44 loc) · 1.67 KB
/
code.py
File metadata and controls
68 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
a = []
b = a
# Remember a and b are _names_ for the list. They both have the _same_ value.
a.append(35) # Modify the value.
print(a)
print(b)
# We mutated (changed) the value, its names still point to the _same thing_, so it doesn't matter which name you use.
a = []
b = []
a.append(35)
print(a)
print(b)
# Here they are different lists, because [] creates a new list every time. You can check whether two things are the _same_ one by usingt the `id()` function:
print(id(a))
print(id(b)) # Different from id(a)
# -- immutable --
# Some values can't be changed because they don't have methods that modify the value itself.
# In case of the list, `.append()` mutates the list.
# For example integers don't have any such methods, so they are called _immutable_.
a = 8597
b = 8597
print(id(a))
print(id(b)) # Same one
a = 8598
print(id(a))
print(
id(b)
) # Different, because we didn't change 8597. We just used the name 'a' for a different value. 'b' still is a name for 8597.
# Most things are mutable in Python. If you want to keep one of your classes immutable, don't add any methods that change the objects' properties.
# Tuples and strings are the only fundamental collection in Python which is immutable.
# Lists, sets, dictionaries are all mutable.
# Integers, floats, and booleans are all immutable.
# -- += and similar --
# A lot of beginners think this:
a = "hello"
b = a
print(id(a))
print(id(b))
a += "world"
# Would cause 'b' to change
# But it doesn't, because strings are immutable. When you do str + str, a _new_ string is created.
# This means that a becomes a new string containing "helloworld", but b still is a name for "hello".
print(id(a))
print(id(b))