Skip to content

ascii

请查看 Python 内建函数列表 了解更多相关 API。

说明:转换为字符串(调用对象的 __repr__ 方法),非 ASCII 字符将被转义。参考 repr 函数。

python
def ascii(obj):
    '''
    转换为字符串(调用对象的 `__repr__` 方法),非 ASCII 字符将被转义

    :param obj: 一个对象
    :return: obj.__repr__ 返回的字符串
    '''

示例:

python
class Cat:
    def __repr__(self) -> str:
        return "I am a 喵喵"

cat = Cat()

print(ascii(cat))