sideeffects

class iteration_utilities.sideeffects(iterable, func, times=0)

Does a normal iteration over iterable and only uses func each times items for it’s side effects.

Parameters:

iterable : iterable

Iterable containing the elements.

func : callable

Function that is called for the side effects.

times : int, optional

Call the function each times items with the last times items. If 0 the argument for func will be the item itself. For any number greater than zero the argument will be a tuple. Default is 0.

Returns:

iterator : generator

A normal iterator over iterable.

Examples

A simple example:

>>> from iteration_utilities import sideeffects
>>> def printit(val):
...     print(val)
>>> list(sideeffects([1,2,3,4], printit))  # in python3 one could use print directly
1
2
3
4
[1, 2, 3, 4]
>>> list(sideeffects([1,2,3,4,5], printit, 2))
(1, 2)
(3, 4)
(5,)
[1, 2, 3, 4, 5]
__length_hint__()

Tries to estimate for the length of the instance (returns 0 if an estimation is not possible).

Note

Requires Python >= 3.4

count

(int) The current count for the next func call (readonly).

New in version 0.6.

func

(callable) The function that is called by sideeffects (readonly).

New in version 0.6.

times

(int) A counter indicating after how many items the func is called (readonly).

New in version 0.6.