def f_no_args():
return 1
assert f_no_args() == 1
def f_one_arg(x):
return x
assert f_one_arg(42) == 42
def add(a, b):
return a + b
assert add(1, 2) == 3
def sum3(a, b, c):
return a + b + c
assert sum3(1, 2, 3) == 6
def f_local():
x = 42
return x
assert f_local() == 42
def f_local_from_arg(x):
y = x + 1
return y
assert f_local_from_arg(10) == 11
def f_local_list():
items = [1, 2, 3]
return items
assert f_local_list() == [1, 2, 3]
def f_local_modify_list():
items = [1, 2]
items.append(3)
return items
assert f_local_modify_list() == [1, 2, 3]
def f_local_multiple():
a = 1
b = 2
c = 3
return a + b + c
assert f_local_multiple() == 6
def f_local_reassign():
x = 1
x = 2
x = 3
return x
assert f_local_reassign() == 3
def nested_basic():
def bar():
return 1
return bar() + 1
assert nested_basic() == 2
def nested_deep():
def level2():
def level3():
return 42
return level3()
return level2()
assert nested_deep() == 42
def nested_multiple_calls():
def inner():
return 10
return inner() + inner() + inner()
assert nested_multiple_calls() == 30
def nested_two_inner():
def add():
return 1
def sub():
return 2
return add() + sub()
assert nested_two_inner() == 3
def nested_with_args(x):
def inner(y):
return y + y
return inner(x) + 1
assert nested_with_args(5) == 11
def eq_test():
return 1
def eq_test2():
return 1
assert eq_test == eq_test
assert not (eq_test != eq_test), 'function not-not-equals itself'
assert not (eq_test == eq_test2), 'different functions not equal'
assert eq_test != eq_test2
f_alias = eq_test
assert f_alias == eq_test
assert eq_test == f_alias
assert len == len
assert print == print
assert not (len != len), 'builtin not-not-equals itself'
assert print is print
assert len is len
assert not (len is print), 'len is not print'
assert not (len == print), 'different builtins not equal'
assert len != print
len_alias = len
assert len_alias == len
assert len_alias is len
assert ValueError == ValueError
assert TypeError == TypeError
assert not (ValueError != ValueError), 'exc type not-not-equals itself'
assert not (ValueError == TypeError), 'different exc types not equal'
assert ValueError != TypeError
exc_alias = ValueError
assert exc_alias == ValueError
def make_adder(n):
def adder(x):
return x + n
return adder
add1 = make_adder(1)
add2 = make_adder(2)
add1_again = make_adder(1)
assert add1 == add1
assert not (add1 != add1), 'closure not-not-equals itself'
assert not (add1 == add1_again), 'different closure instances not equal'
assert add1 != add1_again
assert not (add1 == add2), 'closures with diff captured values not equal'
assert add1 != add2
def cross_test():
return 1
assert not (cross_test == len), 'function not equal to builtin'
assert not (len == cross_test), 'builtin not equal to function'
assert not (cross_test == ValueError), 'function not equal to exc type'
assert not (ValueError == cross_test), 'exc type not equal to function'
assert not (len == ValueError), 'builtin not equal to exc type'
assert not (ValueError == len), 'exc type not equal to builtin'
assert not (len == 1), 'builtin not equal to int'
assert not (len == 'len'), 'builtin not equal to string'
assert not (cross_test == None), 'function not equal to None'
assert not (ValueError == None), 'exc type not equal to None'
x = 5
def shadow_single(x):
return x + 1
assert shadow_single(10) == 11
y = 3
def shadow_multiple(x, y):
return x + y
assert shadow_multiple(20, 30) == 50
def shadow_uses_global_too(x):
return x + y
assert shadow_uses_global_too(100) == 103
def shadow_with_default(x=99):
return x + 1
assert shadow_with_default(10) == 11
assert shadow_with_default() == 100
assert x == 5
assert y == 3
def double(x):
return x * 2
assert double(x) == 10