def sub_sub_gen(): yield 'sub sub yield' return 'sub sub return' def sub_gen(): yield 'sub yield' res = yield from sub_sub_gen() print(f'Print{res}') return 'sub return' def gen(): yield 'gen yield' res = yield from sub_gen() print(f'Print{res}') return 'gen return' gen = gen() print(next(gen)) print(next(gen)) print(next(gen)) print(next(gen)) print(next(gen)) '''実行 結果 gen yield sub yield sub sub yield Printsub sub return Printsub return '''
def bignum(s, b): number = s while number < b: yield number number += 1 for i in bignum(1, 10000000): print(i)
関数間である処理を共通で利用したい場合など
関数オブジェクトを引数にとって、引数にとった関数、実行時に変更を加える
def my_dec(func): def wrap(*args, **kwargs): print('*'*10) func(*args, **kwargs) print('*'*10) return wrap @my_dec def func_a(*args, **kwargs): print('func_a実行') print(args) @my_dec def func_b(*args, **kwargs): print('func_b実行') print(args) func_a(1,2,3) func_b(5,6,7) '''結果 ********** func_a実行 (1, 2, 3) ********** ********** func_b実行 (5, 6, 7) ********** '''