def test_order_of_execution_generator_same_codeline(): test_list = [] expected_list = range(6) def list_append(item): test_list.append(item) def assert_order_of_execution(): print 'expected order', expected_list print 'but got ', test_list assert test_list == expected_list for i in expected_list: #test_list.append will raise a TypeError #yield lambda x, l = test_list: l.append(x), i yield list_append, i yield assert_order_of_execution def test_order_of_execution_generator_different_codeline(): test_list = [] expected_list = range(3) def list_append_2(): test_list.append(2) def list_append_1(): test_list.append(1) def list_append_0(): test_list.append(0) def assert_order_of_execution(): print 'expected order', expected_list print 'but got ', test_list assert test_list == expected_list #yield lambda x, l = test_list: l.append(x), i yield list_append_0 yield list_append_1 yield list_append_2 yield assert_order_of_execution