# Python generators equivalent?

**URL:** <https://forum.elixirforum.com/t/python-generators-equivalent/2806>\
**Category:** Questions\
**Tags:** generators\
**Created:** [December 9, 2016, 10:34am UTC](https://forum.elixirforum.com/t/python-generators-equivalent/2806 "2016-12-09T10:34:08Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![orestis](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/orestis/32/1949_2.png) [@orestis](https://forum.elixirforum.com/u/orestis)\
**Post date:** [December 9, 2016, 1:23pm UTC](https://forum.elixirforum.com/t/python-generators-equivalent/2806/7 "2016-12-09T13:23:31Z")

</div>

Thanks everyone for your thoughts.

I have currently this passing test case:

```elixir
  test "generators" do
    defmodule MyGen do
      use Snakeoil.Generators

      gen "counter", x do
        yield x
        counter(x+1)
      end

      gen "two", {x, y} do
        yield x + y
        two({x+1, y + 1})
      end
    end

    pid = spawn(fn -> MyGen.counter(0) end)
    assert 0 == MyGen.next(pid)
    assert 1 == MyGen.next(pid)
    assert 2 == MyGen.next(pid)

    pid = spawn(fn -> MyGen.two({2, 3}) end)
    assert 5 == MyGen.next(pid)
    assert 7 == MyGen.next(pid)
  end

```

This is a toy, of course - mostly an exercise in metaprogramming. I have to admit that having optional parentheses makes things like that fun to write - but I’m not sure how fun it would be if someone did use that in their own code 🙂 If i’m not mistaken, Elixir also favors explicit over implicit, which is good from me, coming over from Python land.

---

_[View the full topic](https://forum.elixirforum.com/t/python-generators-equivalent/2806)._
