pyxx.arrays.TypedList#
- class pyxx.arrays.TypedList(*values: T, list_type: Type[T], print_multiline: bool = False, multiline_padding: int = 1)#
Bases:
MutableSequence[T]A list whose elements must be of a specific type
This class defines a custom version of Python’s
listobject. When defining theTypedListinstance, users are required to specify a type, and all items added to the list must be of this type. Most of the attributes and methods of the built-inlistobject can be used to manage data in aTypedListinstance.Examples
Create an empty list of integers and add items to it using typical
intmethods:>>> list_of_int = pyxx.arrays.TypedList(list_type=int) >>> list_of_int.append(0) >>> list_of_int.append(2) >>> list_of_int.extend([4, 6, 8]) >>> print(list_of_int) [0, 2, 4, 6, 8]
However, if we try to add items that aren’t of type
int, an exception is thrown:>>> list_of_int.append(1.23) Traceback (most recent call last): ... TypeError: Item 1.23 is not of type <class 'int'>
It’s also possible to convert an existing list into a
TypedList:>>> current_list = [1, 2, 3, 4, 5] >>> typed_list = pyxx.arrays.TypedList(*current_list, list_type=int) >>> print(typed_list) [1, 2, 3, 4, 5]
Attributes
The required type of all items in the list
The number of spaces on either side of the list of items in the list when displaying the list's string representation in multiline format
Whether to display the object's string representation in multiline format
Methods
__init__(*values, list_type[, ...])Creates a
TypedListlist instanceinsert(index, value)Inserts a value at a given index in the list
Inherited Methods
append(value)S.append(value) -- append value to the end of the sequence
clear()count(value)extend(values)S.extend(iterable) -- extend sequence by appending elements from the iterable
index(value, [start, [stop]])Raises ValueError if the value is not present.
pop([index])Raise IndexError if list is empty or index is out of range.
remove(value)S.remove(value) -- remove first occurrence of value.
reverse()S.reverse() -- reverse IN PLACE
- __init__(*values: T, list_type: Type[T], print_multiline: bool = False, multiline_padding: int = 1) None#
Creates a
TypedListlist instanceCreates an instance of the
TypedListobject. This list functions nearly identically to Python’s built-inlist, but it requires that all items in the list be of a user-specified type.- Parameters:
*values (T) – Items to add to the list when initializing the list
list_type (Type[T]) – The required type of all items in the list
print_multiline (bool, optional) – Whether to return a printable string representation of the list in multiline format (default is
False). Multiline format places each item in the list on its own linemultiline_padding (int, optional) – The amount of horizontal padding to place between brackets and list items when creating a printable string representation in multiline format (default is
1). Only applicable ifprint_multilineisTrue
- insert(index: int, value: T)#
Inserts a value at a given index in the list
- property list_type: Type[T]#
The required type of all items in the list
- append(value)#
S.append(value) – append value to the end of the sequence
- clear() None -- remove all items from S#
- count(value) integer -- return number of occurrences of value#
- extend(values)#
S.extend(iterable) – extend sequence by appending elements from the iterable
- 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.
- pop([index]) item -- remove and return item at index (default last).#
Raise IndexError if list is empty or index is out of range.
- property print_multiline: bool#
Whether to display the object’s string representation in multiline format
- remove(value)#
S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.
- reverse()#
S.reverse() – reverse IN PLACE
- property multiline_padding: int#
The number of spaces on either side of the list of items in the list when displaying the list’s string representation in multiline format