[omaha] Iterators vs Generator!!
Varun Mehta
varun.sagi20 at gmail.com
Tue Oct 28 17:26:58 CET 2014
Thank you Jeff for the crisp inputs.
That is exactly what i was looking for.
Varun
On Oct 27, 2014, at 11:09 PM, Jeff Hinrichs - DM&T <jeffh at dundeemt.com> wrote:
> The primary difference is that generators are not reusable. see
> http://pymbook.readthedocs.org/en/latest/igd.html for additional
> information.
>
> essentially, when you hit the end of your generator, because it holds
> state, it keeps saying, "stop iteration" by returing None and not yield'ing
> a value. Where an iterator has no state. When it goes out of scope, like
> in a "for" block, the next time you use it, it starts over.
>
> You can make a generator reusable, but it takes work and purpose, since you
> are working against it's reason for existing and attempting to turn it back
> into a more general iterator.
>
> Best,
> Jeff
>
> On Mon, Oct 27, 2014 at 7:19 PM, Varun Mehta <varun.sagi20 at gmail.com> wrote:
>
>> Thank you for inputs Wes.
>>
>> I guess i’ll find my answers in one of the links.
>>
>> -Varun
>>
>> On Oct 27, 2014, at 7:07 PM, Wes Turner <wes.turner at gmail.com> wrote:
>>
>>> Both iterators and generators can be functionally composed.
>>>
>>> Generators can maintain local state as they are iterated through.
>>>
>>> Learning not to consume iterators and generators is a good thing; both
>> for
>>> Python 3 (where things like .keys() are now .iterkeys()) and for asyncio
>>> ("yield from").
>>>
>>> An object can implement .__iter__() /.next()) and not .__len__().
>>>
>>> Docs:
>>>
>>>
>> https://docs.python.org/2/reference/datamodel.html#the-standard-type-hierarchy
>>>
>>> https://docs.python.org/2/glossary.html#term-generator
>>>
>>> https://docs.python.org/2/glossary.html#term-iterator
>>>
>>> https://docs.python.org/2/tutorial/classes.html#generators
>>>
>>> https://docs.python.org/2/howto/functional.html#iterators
>>>
>>> https://docs.python.org/2/tutorial/classes.html#iterators
>>>
>>> https://docs.python.org/2/library/itertools.html
>>>
>>>
>> https://scipy-lectures.github.io/advanced/advanced_python/index.html#iterators-generator-expressions-and-generators
>>>
>>> https://docs.python.org/3/library/asyncio.html (3.4+)
>>>
>>> Tools for working functionally with iterators and generators:
>>>
>>> https://github.com/pytoolz/toolz
>>>
>>> https://github.com/kachayev/fn.py/blob/master/README.rst
>>>
>>> http://funcy.readthedocs.org/en/latest/
>>> On Oct 27, 2014 4:02 PM, "Varun Mehta" <varun.sagi20 at gmail.com> wrote:
>>>
>>>> Hi all
>>>>
>>>> I have a query regarding the Iterator vs Generator.
>>>> Generator is specific type of Iterator.
>>>> But both still use next function to produce next value and save memory.
>>>>
>>>> So, which should be preferred?
>>>> Any advantage of one over other?
>>>>
>>>> Warm Regards
>>>> Varun Mehta
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Omaha Python Users Group mailing list
>>>> Omaha at python.org
>>>> https://mail.python.org/mailman/listinfo/omaha
>>>> http://www.OmahaPython.org
>>>>
>>> _______________________________________________
>>> Omaha Python Users Group mailing list
>>> Omaha at python.org
>>> https://mail.python.org/mailman/listinfo/omaha
>>> http://www.OmahaPython.org
>>
>> _______________________________________________
>> Omaha Python Users Group mailing list
>> Omaha at python.org
>> https://mail.python.org/mailman/listinfo/omaha
>> http://www.OmahaPython.org
>>
>
>
>
> --
> Best,
>
> Jeff Hinrichs
> 402.218.1473
> _______________________________________________
> Omaha Python Users Group mailing list
> Omaha at python.org
> https://mail.python.org/mailman/listinfo/omaha
> http://www.OmahaPython.org
More information about the Omaha
mailing list