Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82278 > unrolled thread
| Started by | Shiyao Ma <i@introo.me> |
|---|---|
| First post | 2014-12-10 18:03 +0800 |
| Last post | 2014-12-11 01:38 +1100 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Nested loops is strangely slow, totally at a loss. Shiyao Ma <i@introo.me> - 2014-12-10 18:03 +0800
Re: Nested loops is strangely slow, totally at a loss. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-11 01:38 +1100
| From | Shiyao Ma <i@introo.me> |
|---|---|
| Date | 2014-12-10 18:03 +0800 |
| Subject | Re: Nested loops is strangely slow, totally at a loss. |
| Message-ID | <mailman.16806.1418207323.18130.python-list@python.org> |
Thanks guys. I was only aware of a limited iterables which themselves are iterators, e.g., the generator. Seems like its really a pitfall. Any glossary, list on the iterables that *might* exhaust themselves? Regards.
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-12-11 01:38 +1100 |
| Message-ID | <54885b03$0$12981$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #82278 |
Shiyao Ma wrote:
> Thanks guys.
>
> I was only aware of a limited iterables which themselves are iterators,
> e.g., the generator.
>
> Seems like its really a pitfall. Any glossary, list on the iterables that
> *might* exhaust themselves?
Iterables include:
- iterators
- sequences (e.g. lists, tuples, xrange objects)
- collections such as dicts, sets, etc.
- strings (byte strings and unicode strings)
Only iterators can be exhausted, since they generate their values on-call,
not in advance, and once a value is extracted, it's gone.
Iterators are not a type or class, they are objects which obey the iterator
protocol. In other words, iterators are duck-typed, not all instances of
the same type.
How do you tell if something is an iterator? By duck-typing:
# is obj an iterator?
if iter(obj) is obj and hasattr(obj, "__next__"):
# in Python 2, use "next" instead of "__next__"
print("obj is an iterator")
In Python 3 you can also use an Abstract Base Class:
from collections.abc import Iterator
if isinstance(obj, Iterator):
print("obj is an iterator")
You can also get a hint from the name:
py> print iter([])
<listiterator object at 0xb7bf690c>
A built-in type that claims to be a "something iterator" is very probably an
iterator.
Calling iter() on any iterable will return an iterator.
Generators are also iterators.
In Python 2, dicts have methods .iterkeys(), .itervalues(), .iteritems()
which return iterators.
Anything you get from the itertools module is likely to be an iterator.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web