class menpo.base.LazyList(callables)[source]

Bases: Sequence, Copyable

An immutable sequence that provides the ability to lazily access objects. In truth, this sequence simply wraps a list of callables which are then indexed and invoked. However, if the callable represents a function that lazily access memory, then this list simply implements a lazy list paradigm.

When slicing, another LazyList is returned, containing the subset of callables.


callables (list of callable) – A list of callable objects that will be invoked if directly indexed.


Generate an efficient copy of this LazyList - copying the underlying callables will be lazy and shallow (each callable will not be called nor copied) but they will reside within in a new list.


type(self) – A copy of this LazyList.

count(value) → integer -- return number of occurrences of value
index(value[, start[, stop]]) → integer -- return first index of value.

Raises ValueError if the value is not present.

Supporting start and stop arguments is optional, but recommended.

classmethod init_from_index_callable(f, n_elements)[source]

Create a lazy list from a callable that expects a single parameter, the index into an underlying sequence. This allows for simply creating a LazyList from a callable that likely wraps another list in a closure.

  • f (callable) – Callable expecting a single integer parameter, index. This is an index into (presumably) an underlying sequence.

  • n_elements (int) – The number of elements in the underlying sequence.


lazy (LazyList) – A LazyList where each element returns the underlying indexable object wrapped by f.

classmethod init_from_iterable(iterable, f=None)[source]

Create a lazy list from an existing iterable (think Python list) and optionally a callable that expects a single parameter which will be applied to each element of the list. This allows for simply creating a LazyList from an existing list and if no callable is provided the identity function is assumed.

  • iterable (collections.Iterable) – An iterable object such as a list.

  • f (callable, optional) – Callable expecting a single parameter.


lazy (LazyList) – A LazyList where each element returns each item of the provided iterable, optionally with f applied to it.


Create a new LazyList where the passed callable f wraps each element.

f should take a single parameter, x, that is the result of the underlying callable - it must also return a value. Note that mapping is lazy and thus calling this function should return immediately.

Alternatively, f may be a list of callable, one per entry in the underlying list, with the same specification as above.


f (callable or iterable of callable) – Callable to wrap each element with. If an iterable of callables (think list) is passed then it must by the same length as this LazyList.


lazy (LazyList) – A new LazyList where each element is wrapped by (each) f.


Repeat each item of the underlying LazyList n times. Therefore, if a list currently has D items, the returned list will contain D * n items and will return immediately (method is lazy).


n (int) – The number of times to repeat each item.


lazy (LazyList) – A LazyList where each element returns each item of the provided iterable, optionally with f applied to it.


>>> from menpo.base import LazyList
>>> ll = LazyList.init_from_list([0, 1])
>>> repeated_ll = ll.repeat(2)  # Returns immediately
>>> items = list(repeated_ll)   # [0, 0, 1, 1]

Visualize this lazy collection of items using menpowidgets.

The type of the first item will be used to determine an appropriate visualization for the list of items.


widget – The appropriate menpowidget to view these items

  • MenpowidgetsMissingError – If menpowidgets is not installed

  • ValueError – If menpowidgets cannot locate an appropriate items-visualization for the type of items in this LazyList