mt.base.casting
Registry to store all the cast functions to cast an object from type A to type B.
Functions
cast()
: Casts an object to a target type.castable()
: Checks if an object can be cast to a target type.register_cast()
: Registers a function to cast an object from type A to type B.register_castable()
: registers a function to check if we can cast an object from type A to type B.
- mt.base.casting.cast(obj, dst_type)
Casts an object to a target type.
- Parameters:
obj (object) – object to be cast
dst_type (type) – type or class to cast to
- Returns:
the cast version of obj
- Return type:
object
- Raises:
NotImplementedError – if the cast function has not been registered using function register_cast
- mt.base.casting.castable(obj, dst_type)
Checks if an object can be cast to a target type.
- Parameters:
obj (object) – object to be cast
dst_type (type) – type or class to cast to
- Returns:
whether or not the object is castable to dst_type
- Return type:
bool
- mt.base.casting.register_cast(src_type, dst_type, func)
Registers a function to cast an object from type A to type B.
- Parameters:
src_type (type) – type or class to cast from
dst_type (type) – type or class to cast to
func (function) – cast function to register
- mt.base.casting.register_castable(src_type, dst_type, func)
registers a function to check if we can cast an object from type A to type B.
- Parameters:
src_type (type) – type or class to cast from
dst_type (type) – type or class to cast to
func (function) – castable function to register