Skip to content

Instantly share code, notes, and snippets.

def do_something(some_list=[]):
some_list.append('some item')
print some_list
>>> do_something()
['some item']
>>> do_something()
['some item', 'some item']
>>> do_something()
['some item', 'some item', 'some item']
>>> do_something()
['some item', 'some item', 'some item', 'some item']
>>>
>>> do_something(['my item'])
['my item', 'some item']
>>> do_something(['my item'])
['my item', 'some item']
>>> do_something(['my item'])
['my item', 'some item']
>>>
>>> do_something.func_defaults
(['some item'],)
>>>
>>> do_something.func_defaults = ([42],)
>>> do_something()
[42, 'some item']
>>>
from contextlib import contextmanager
import sys, os
@contextmanager
def suppress_stdout():
with open(os.devnull, "w") as devnull:
old_stdout = sys.stdout
sys.stdout = devnull
try:
yield
print "You can see this"
with suppress_stdout():
print "You cannot see this"
print "And you can see this again"
import itertools
for a, b, c, d, e, f in itertools.product(range(1, 10), range(1, 10), range(1, 10), range(1, 10), range(1, 10), range(1, 10)):
if (a + b + c == 18 and
d + e + f == 18 and
a + d == b + e and
b + e == c + f and
int(str(e) + str(f)) in [16, 25, 36, 49, 64, 81] and
len(set([a, b, c, d, e, f])) == 6):
print a, b, c, d, e, f
scale 562949953421312
bad at 1125899906842624 0x4000000000000 0b100000000000000000000000000000000000000000000000000
scale 281474976710656
bad at 562949953421312 0x2000000000000 0b10000000000000000000000000000000000000000000000000
scale 140737488355328
bad at 281474976710656 0x1000000000000 0b1000000000000000000000000000000000000000000000000
scale 70368744177664
void my_function() {
char *data = malloc(1024);
return; // oops! memory leak
}