TypeError: unsupported format string passed to test.__format__
在Python中,如果对未定义__format__()
方法的对象使用format()
函数,就会造成TypeError: unsupported format string passed to test.__format__错误;
正确的代码
class test:
def __format__(self,format_spec=''):
return str(123) #返回值必须是一个字符串
a = test()
print(format(a))