タグ

Pythonとgeneratorに関するarveltのブックマーク (2)

  • What does the "yield" keyword do in Python?

    Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. What functionality does the yield keyword in Python provide? For example, I'm trying to understand this code1: def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distanc

    What does the "yield" keyword do in Python?
  • Pythonのイテレータとジェネレータ - Qiita

    Pythonのイテレータ(iterator)とジェネレータ(generator)についてまとめてみます。 (追記2018.12.25: Python3の文法に全面的に置き換えました) イテレータ: 要素を反復して取り出すことのできるインタフェース ジェネレータ: イテレータの一種であり、1要素を取り出そうとする度に処理を行い、要素をジェネレートするタイプのもの。Pythonではyield文を使った実装を指すことが多いと思われる Python組み込みのコレクション(list, tuple, set, dictなど)はどれもイテレーション可能ですが、組み込みのコレクションを使った繰り返し処理ではあらかじめコレクションに値を入れておく必要があるため、以下のようなケースではイテレータやジェネレータを自分で実装したいというケースがあると思います。 無限に繰り返すイテレーション 要素すべてをあらかじめ

    Pythonのイテレータとジェネレータ - Qiita
  • 1