以下の内容はhttps://t2y.hatenablog.jp/entry/20100902/1283426815より取得しました。


0と1を次々返す方法の実装

0と1を次々返す方法 - When it’s ready. を考えてみました。私だったらこう実装するかな。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from operator import not_

def toggle(obj):
    while True:
        obj = not_(obj)
        yield obj

def main():
    g = toggle(True)
    print g.next()
    print g.next()
    print g.next()
    print g.next()
    print g.next()

if __name__ == '__main__':
    main()

実行結果。

$ python toggle_sample.py 
False
True
False
True
False

リファレンス:




以上の内容はhttps://t2y.hatenablog.jp/entry/20100902/1283426815より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14