TypedList

class ipkiss3.all.TypedList

A list that ensures that all elements in it are of a given type.

The TypedList class can be used to create lists to which you can only add objects that are instances of a certain class. By default, TypedList allows all python objects. If you want to restrict to a certain type, you’ll have to extend TypedList.

The class below will for example only allow python strings:

class StrList(TypedList):
    _item_type = str

It’s important to note that TypedList and its subclasses are not real lists, at least not in the sense that it’s a subclass of the python list object:

>>> isinstance(TypedList([1, 2, 3]), list)
False

It does however have the same functionality of normal list, so you can append, update or slice the list as you could do with regular lists.

Examples

>>> from ipkiss3 import all as i3
>>> class StrList(i3.TypedList):
...     _item_type = str
>>> abc = ['a', 'b', 'c']
>>> strlst = StrList(abc)
>>> strlst.append('d')
>>> strlst.extend(list('efg'))
>>> strlst[2:]
['c', 'd', 'e', 'f', 'g']
data

A member which allows list values.

Assigning to a list creates a copy. The orginal list will remain unmodified. This is similar to the semantics of the assignment operator on the C++ STL container classes.