astype

emlib.misc.astype(type_, obj=None, factory=None)[source]

Return obj as type.

If obj is already of said type, obj itself is returned Otherwise, obj is converted to type. If a special contructor is needed, it can be given as construct. If no obj is passed, a partial function is returned which can check for that particular type

Parameters:
  • type – the type the object should have

  • obj – the object to be checkec/converted

  • factory – if given, a function (obj) -> obj of type type_

Example

>>> astype(list, (3, 4))
[3, 4]
>>> l = [3, 4]
>>> astype(list, l) is l
True
>>> aslist = astype(list)