Skip to content

Instantly share code, notes, and snippets.

@rciorba
Forked from vtemian/gotcha.py
Last active August 29, 2015 14:20
Show Gist options
  • Save rciorba/ffbedcb3c2233521b593 to your computer and use it in GitHub Desktop.
Save rciorba/ffbedcb3c2233521b593 to your computer and use it in GitHub Desktop.
# mutable list
def foobar(arg_string="abc", arg_list=[]):
print arg_string, arg_list
arg_string = arg_string + "xyz"
arg_list.append("F")
for i in range(4):
foobar()
"""
results
abc []
abc ['F']
abc ['F', 'F']
abc ['F', 'F', 'F']
"""
# late binding
def create_multipliers():
return [lambda x : i * x for i in range(5)]
for multiplier in create_multipliers():
print multiplier(2)
"""
8
8
8
8
8
"""
class Foo(object):
a = 1
foo = Foo()
foo.a = 2
Foo.a = 3
print(foo.a)
print(type(foo).a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment