robturtle
1/4/2017 - 5:54 PM

Gotchas in Python.py

#!/usr/bin/env python3

l = [set()] * 5 # All the elements are pointing to the same object!

def fuckme(arg = []):
  arg.append(1)
  print(arg)
for i in range(3):
  fuckme() # all the invokations the default args are pointing to the same object!
  
def outer():
  counter = 0
  def inner():
    counter += 1 # No you can't mutate the outer variable! So that's why we have `nonlocal` keyword...