Mar*_*ers 15
是的,自定义类可能会失败:
>>> class C(object):
... def __str__(self):
... return 'oops: ' + oops
...
>>> c = C()
>>> str(c)
NameError: global name 'oops' is not defined
Run Code Online (Sandbox Code Playgroud)
对于某些内置类,它甚至可能会失败,例如unicode:
>>> u = u'\xff'
>>> s = str(u)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in position 0:
ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
Sve*_*ach 10
当然是:
class A(object):
def __str__(self):
raise Exception
a = A()
str(a)
Run Code Online (Sandbox Code Playgroud)