2.2. ListView class

class immutable_views.ListView(a_list)[source]

An immutable list view.

Derived from Sequence.

This class provides an immutable view on a possibly mutable sequence object. The sequence object must be an instance of Sequence, e.g. list, tuple, range, or a user-defined class.

This can be used for example when a class maintains a list that should be made available to users of the class without allowing them to modify the list.

In the description of this class, the term ‘view’ always refers to the ListView object, and the term ‘list’ or ‘underlying list’ refers to the sequence object the view is based on.

The ListView class supports the complete behavior of Python class list, except for any methods that would modify the list. Note that the non-modifying methods of class list are a superset of the methods defined for the abstract class Sequence (the methods are listed in the table at the top of the linked page).

The view is “live”: Since the view class delegates all operations to the underlying list, any modification of the underlying list object will be visible in the view object.

Note that only the view object is immutable, not its items. So if the values in the underlying list are mutable objects, they can be modified through the view.

Note that in Python, augmented assignment (e.g. x += y) is not guaranteed to modify the left hand object in place, but can result in the left hand name being bound to a new object (like in x = x + y). For details, see object.__iadd__().

For the ListView class, augmented assignment is supported and results in binding the left hand name to a new ListView object.

Parameters

a_list (Sequence) – The underlying list. If this object is a ListView, its underlying list is used.

Methods:

__add__(other)

self + other: Return a new view on the concatenation of the list and the other list.

__contains__(value)

value in self: Return a boolean indicating whether the list contains a value.

__eq__(other)

self == other: Return a boolean indicating whether the list is equal to the other list.

__ge__(other)

self < other: Return a boolean indicating whether the list is greater than or equal to the other list.

__getitem__(index)

self[index]:

__getstate__()

Support for pickling.

__gt__(other)

self > other: Return a boolean indicating whether the list is greater than the other list.

__hash__()

hash(self): Return a hash value for the list.

__iter__()

Return an iterator through the list items.

__le__(other)

self < other: Return a boolean indicating whether the list is less than or equal to the other list.

__len__()

len(self): Return the number of items in the list.

__lt__(other)

self < other: Return a boolean indicating whether the list is less than the other list.

__mul__(number)

self * number: Return a new view on the multiplication of the list with a number.

__ne__(other)

self != other: Return a boolean indicating whether the list is not equal to the other list.

__repr__()

repr(self): Return a string representation of the view suitable for debugging.

__reversed__()

reversed(self) ...: Return an iterator through the list in reversed iteration order.

__rmul__(number)

number * self: Return a new view on the multiplication of the list with a number.

__setstate__(a_dict)

Support for unpickling.

copy()

Return a new view on a shallow copy of the list.

count(value)

Return the number of times the specified value occurs in the list.

index(value[, start, stop])

Return the index of the first item in the list with the specified value.

Attributes:

list

The underlying list.

__add__(other)[source]

self + other: Return a new view on the concatenation of the list and the other list.

The returned ListView object is a view on a new list object of the type of the left hand operand that contains the items that are in the underlying list of the left hand operand, concatenated with the items in the other list (or in case of a ListView, its underlying list).

The other object must be an iterable or ListView.

The list and the other list are not changed.

Raises

TypeError – The other object is not an iterable.

__contains__(value)[source]

value in self: Return a boolean indicating whether the list contains a value.

The return value indicates whether the underlying list contains an item that is equal to the value.

__eq__(other)[source]

self == other: Return a boolean indicating whether the list is equal to the other list.

The return value indicates whether the items in the underlying list are equal to the items in the other list (or in case of a ListView, its underlying list).

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__ge__(other)[source]

self < other: Return a boolean indicating whether the list is greater than or equal to the other list.

The return value indicates whether the underlying list is greater than or equal to the other list (or in case of a ListView, its underlying list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__getitem__(index)[source]
self[index]:

Return the list value at the index position.

Raises

IndexError – Index out of range.

__getstate__()[source]

Support for pickling.

__gt__(other)[source]

self > other: Return a boolean indicating whether the list is greater than the other list.

The return value indicates whether the underlying list is greater than the other list (or in case of a ListView, its underlying list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__hash__()[source]

hash(self): Return a hash value for the list.

Whether hashing is supported depends on the underlying list. For example, the standard Python list class does not support hashing, but the standard Python tuple class does.

Raises

TypeError – The underlying list does not support hashing.

__iter__()[source]

Return an iterator through the list items.

__le__(other)[source]

self < other: Return a boolean indicating whether the list is less than or equal to the other list.

The return value indicates whether the underlying list is less than or equal to the other list (or in case of a ListView, its underlying list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__len__()[source]

len(self): Return the number of items in the list.

The return value is the number of items in the underlying list.

__lt__(other)[source]

self < other: Return a boolean indicating whether the list is less than the other list.

The return value indicates whether the underlying list is less than the other list (or in case of a ListView, its underlying list), based on the lexicographical ordering Python defines for sequence types (see https://docs.python.org/3/tutorial/datastructures.html#comparing-sequences-and-other-types)

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__mul__(number)[source]

self * number: Return a new view on the multiplication of the list with a number.

The returned ListView object is a view on a new list object of the type of the left hand operand that contains the items that are in the underlying list of the left hand operand as many times as specified by the right hand operand.

A number <= 0 causes the returned list to be empty.

The left hand operand is not changed.

__ne__(other)[source]

self != other: Return a boolean indicating whether the list is not equal to the other list.

The return value indicates whether the items in the underlying list are not equal to the items in the other list (or in case of a ListView, its underlying list).

The other object must be a list or ListView.

Raises

TypeError – The other object is not a list or ListView.

__repr__()[source]

repr(self): Return a string representation of the view suitable for debugging.

The underlying list is represented using its repr() representation.

__reversed__()[source]

reversed(self) ...: Return an iterator through the list in reversed iteration order.

The returned iterator yields the items in the underlying list in the reversed iteration order.

__rmul__(number)[source]

number * self: Return a new view on the multiplication of the list with a number.

This method is a fallback and is called only if the left operand does not support the operation.

The returned ListView object is a view on a new list object of the type of the right hand operand that contains the items that are in the underlying list of the right hand operand as many times as specified by the left hand operand.

A number <= 0 causes the returned list to be empty.

The right hand operand is not changed.

__setstate__(a_dict)[source]

Support for unpickling.

copy()[source]

Return a new view on a shallow copy of the list.

The returned ListView object is a new view object on a list object of the type of the underlying list.

If the list type is immutable, the returned list object may be the underlying list object. If the list type is mutable, the returned list is a new list object that is a shallow copy of the underlying list object.

count(value)[source]

Return the number of times the specified value occurs in the list.

index(value, start=0, stop=9223372036854775807)[source]

Return the index of the first item in the list with the specified value.

The search is limited to the index range defined by the specified start and stop parameters, whereby stop is the index of the first item after the search range.

Raises

ValueError – No such item is found.

property list

The underlying list.

Access to the underlying list is provided for purposes such as conversion to JSON or other cases where the view classes do not work. This access should not be used to modify the underlying list.