指定されたステップ関数に基づいて値の範囲をループできる Python スクリプトを作成しました。
#!/usr/bin/python
def mul(value, step): return value * step
def inc(value, step): return value + step
def step_range(start, end, step, func):
while start <= end:
yield start
start = func(start, step)
def main():
for x in step_range(1, 64, 2, mul):
print '%d, '%(x),
print
for x in step_range(0, 64, 8, inc):
print '%d, '%(x),
if __name__ == '__main__':
main()
出力:
$ python test.py
1, 2, 4, 8, 16, 32, 64,
0, 8, 16, 24, 32, 40, 48, 56, 64,
ユーザーがこのようなことをできるように、ヘルパー関数を取り除くことができる方法はありますか?
for x in step_range(1, 64, *2):
...
def step_range(start, end, step):
while start <= end:
yield start
start = start ?step?
?
私が困惑しているマーク... モジュールを調べましたが、と関数operator
の両方のパラメーターを知る必要があります。mul(a, b)
add(a, b)