3 ms·
Author here! Very true, though I was trying to focus on idiomatic python that pushes logic into for loops, list comprehensions, etc w/ composable iterators thro
by softwaredoug 5y ago
Author here! Very true, though I was trying to focus on idiomatic python that pushes logic into for loops, list comprehensions, etc w/ composable iterators through tools like those found in itertools (zip, etc...)
Since Python doesn't have Ruby's blocks, you have to define a non-trivial function as a free function, so it's less likely you'll do this. (Python's lambda's are far more limited than Ruby's blocks.)
You can also trick Ruby to return a new value every time a method is called for iteration, but again my focus on what I see as idiomatic in the two languages
- gvx 5y agoYour Python example is far from idiomatic, though. If a Python programmer were forced to write `Stuff`, they'd write something like: class Stuff: def __init__(self): self.a_list = [1,2,3,4] def __iter__(self): return iter(self.a_list) --- or do what the other person did, and make `__iter__` a generator, which isn't necessary in this case but is much more flexible.
- softwaredoug 5y agoYes, but if you’re new to Python you’re not going to grok this.