Gotchas
Gotcha.
Using a single equal sign = for a conditional instead of double equal signs ==.
The single equal sign = is for assignment.
if x = 1: # Syntax error
print("x is 1")
Gotcha.
Division defaults to floats.
For integer division, use double forward-slashes //.
x = 5 / 2 # `x` is `2.5`
y = 5 // 2 # `y` is `2`
Gotcha. Using a built-in name as a variable.
str = "A string" # Shadows built-in `str`
isinstance("Another string", str) # Type error
Gotcha.
Classes are passed by reference in functions.
That is, functions modify the class outside its scope.
Often, this is the desired behavior.
When it is not, make a copy with the library copy.
def append_to_list(x):
x.append(3)
return x
lst = [1, 2]
append_to_list(lst)
print(lst) # prints [1, 2, 3]