Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 690 Bytes

File metadata and controls

42 lines (30 loc) · 690 Bytes

Pitfalls

UnboundLocalError

count = 0

def increment():
    count += 1  # UnboundLocalError
    # global count
    # count = count + 1

increment()
increment()
increment()
increment()
increment()

print(count)

Default lists

def append_to_list(value, my_list=[]):
    my_list.append(value)
    return my_list

print(append_to_list(1))  # Output: [1]
print(append_to_list(2))  # Output: [1, 2] (unexpected)

Shadowing

list = [1, 2, 3, 4, 5]  # Shadowing the built-in 'list'

# Trying to use the built-in list function now will result in an error
new_list = list((6, 7, 8))  # TypeError: 'list' object is not callable
print(new_list)