[Corso Python3] ancora...

alessandro medici alexxandro.medici a gmail.com
Mer 15 Giu 2016 23:55:57 CEST


iterableAn object capable of returning its members one at a time. Examples
of iterables include all sequence types (such as list
<https://docs.python.org/3.6/library/stdtypes.html#list>, str
<https://docs.python.org/3.6/library/stdtypes.html#str>, and tuple
<https://docs.python.org/3.6/library/stdtypes.html#tuple>) and some
non-sequence types like dict
<https://docs.python.org/3.6/library/stdtypes.html#dict>,file objects
<https://docs.python.org/3.6/glossary.html#term-file-object>, and objects
of any classes you define with an __iter__()
<https://docs.python.org/3.6/reference/datamodel.html#object.__iter__> or
__getitem__()
<https://docs.python.org/3.6/reference/datamodel.html#object.__getitem__>
method.
Iterables can be used in a for
<https://docs.python.org/3.6/reference/compound_stmts.html#for> loop and in
many other places where a sequence is needed (zip()
<https://docs.python.org/3.6/library/functions.html#zip>, map()
<https://docs.python.org/3.6/library/functions.html#map>, ...). When an
iterable object is passed as an argument to the built-in function iter()
<https://docs.python.org/3.6/library/functions.html#iter>, it returns an
iterator for the object. This iterator is good for one pass over the set of
values. When using iterables, it is usually not necessary to call iter()
<https://docs.python.org/3.6/library/functions.html#iter> or deal with
iterator objects yourself. The for statement does that automatically for
you, creating a temporary unnamed variable to hold the iterator for the
duration of the loop. See also iterator
<https://docs.python.org/3.6/glossary.html#term-iterator>, sequence
<https://docs.python.org/3.6/glossary.html#term-sequence>, and generator
<https://docs.python.org/3.6/glossary.html#term-generator>.iterator

An object representing a stream of data. Repeated calls to the iterator’s
__next__()
<https://docs.python.org/3.6/library/stdtypes.html#iterator.__next__> method
(or passing it to the built-in function next()
<https://docs.python.org/3.6/library/functions.html#next>) return
successive items in the stream. When no more data are available a
StopIteration
<https://docs.python.org/3.6/library/exceptions.html#StopIteration> exception
is raised instead. At this point, the iterator object is exhausted and any
further calls to its __next__() method just raiseStopIteration
<https://docs.python.org/3.6/library/exceptions.html#StopIteration> again.
Iterators are required to have an __iter__()
<https://docs.python.org/3.6/reference/datamodel.html#object.__iter__> method
that returns the iterator object itself so every iterator is also iterable
and may be used in most places where other iterables are accepted. One
notable exception is code which attempts multiple iteration passes. A
container object (such as a list
<https://docs.python.org/3.6/library/stdtypes.html#list>) produces a fresh
new iterator each time you pass it to the iter()
<https://docs.python.org/3.6/library/functions.html#iter> function or use
it in a for <https://docs.python.org/3.6/reference/compound_stmts.html#for>
loop.
Attempting this with an iterator will just return the same exhausted
iterator object used in the previous iteration pass, making it appear like
an empty container.

More information can be found in Iterator Types
<https://docs.python.org/3.6/library/stdtypes.html#typeiter>.
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: <http://lists.fsugpadova.org/pipermail/fsug-corso-python3/attachments/20160615/5e1ed583/attachment.html>


Maggiori informazioni sulla lista fsug-corso-python3