python the next () function

next (iterobject, defalt) the first parameter is a function of the object can be iterative, the second parameter may not be written. When not writing, if iterables elements out is completed, it will return StopIteration. If the second argument to write one of the other elements, iteration after iteration object can be completed, it will always return that element to write.

example:

b=[1,2,3,4]

a=iter(b)

next (a) will be returned in turn 1,2,3,4, StopIteration, StopIteration ...

a=iter(b,19)

next (a) will in turn return 1,2,3,4,19,19,19 ...

 Note: Generator (Generator) is an iterator object.
example:

c=(i for i in rang(5))

type (c) returns: generator

next (c) returned in turn: 0,1,2,3,4, StopIteration, StopIteration ...

 

Guess you like

Origin www.cnblogs.com/yibeimingyue/p/11275076.html