Python 中是否存在不能在字符串中类型转换的数据类型 [英] Is there a Data Type in Python which cannot be typecast in string

查看:22
本文介绍了Python 中是否存在不能在字符串中类型转换的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设 'a' 是某种类型(我想知道!)

Lets say 'a' is of some type (which I want to know!)

做这样的事情:

b = str(a)

这应该有利地引发 TypeError

推荐答案

没有内置的 Python 类会引发带有 str 的 TypeError,但您可以定义一个自定义类:

There is no builtin Python class that raises a TypeError with str, but you could define a custom class:

class Foo(object):
    def __str__(self):
        raise TypeError('Can not by stringified')


foo = Foo()
b = str(foo)

引发 TypeError.

这篇关于Python 中是否存在不能在字符串中类型转换的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆