def text ( value , encoding = "utf-8" , errors = "strict" ) : if isinstance ( value , text_type ) : return value elif isinstance ( value , bytes ) : return text_type ( value , encoding , errors ) else : return text_type ( value )
python cast string to custom type
Convert a value to str on Python 3 and unicode on Python 2 .
def text(value, encoding="utf-8", errors="strict"):
    """Convert a value to str on Python 3 and unicode on Python 2."""
    if isinstance(value, text_type):
        return value
    elif isinstance(value, bytes):
        return text_type(value, encoding, errors)
    else:
        return text_type(value)