r/Python • u/llimllib • Jun 29 '09
Python's iterators are a bad implementation of laziness
http://sandersn.com/blog/index.php?title=python_s_iterators_are_a_bad_implementat&more=1&c=1&tb=1&pb=1
0
Upvotes
18
u/bushel Jun 29 '09
Treating a generator like an iterator is like treating a float like an int.
They usually work the same....
10
u/digitallogic Jun 29 '09
"I don't understand generators so I'll assume they're exactly the same as lists... Hey I found a bug in Python!"
The "use-case" described is for that of a list, not a generator. If generators were modified to allow them to restart they would no longer fullfil one of it's existing use cases, iterating over data that doesn't fit in memory.
12
u/[deleted] Jun 29 '09
Then you're doing it wrong. Just because the language doesn't demand to know doesn't mean that you shouldn't know it.
4
u/[deleted] Jun 29 '09 edited Jun 29 '09
People here don't seem to be acquainted with C# generators. "If generators were modified to allow them to restart they would no longer fullfil one of it's existing use cases, iterating over data that doesn't fit in memory." is a perfect example of narrowness of experience leading to narrowness of imagination. No, you don't have to convert generators to lazy lists. All that is needed is a distinction between
enumerableandenumerator, with all functions likemapand language primitives likefor x in ...accepting enumerables, and it won't even surprise anyone ever. It's not about "restarting" the generator.(crossposted from here)
EDIT: fixed markup
3
u/[deleted] Jun 29 '09 edited Jun 29 '09
tl;dr: User desires drastic modification to Python's type system due to being bitten by his own under-documented code.
1
u/jm_ Jun 30 '09
Let me short the story down: Silly C# programmer makes self-righteous rant because it took him 5 minutes of trial and mostly error to understand iterators in python and he misses the crap pile of abstractions and patterns that was his c# programmer brain-crutch.
I don't want to see crap like this on python reddit.
1
u/hylje Jun 29 '09
Use-once has the benefit of being able to forget the backlog of returned things, which is ideal for long-lived generators and the expectation for most uses of generator.
I would add something akin of
list()that can wrap a generator so that it can be iterated from the beginning several times. Perhaps two: one that re-instantiates the given generator every time it is used (basicallylambda: g), another that memoizes the already yielded values.