在 Visual Studio Code 中,可以将变量添加到监视面板;但是,为自定义类显示的值似乎是类型而不是类实例的str (...) 表示。
考虑这个例子:
class MyCustomObject(object):
state = "I'm beautiful and comprehensive!"
def __str__(self):
return self.state
instance = MyCustomObject()
print(instance)
如果我添加instance
到监视窗口,我看到的是:
instance: <__main__.MyCustomObject object at 0x0000000001234>
但我想看到的是:
instance: 'I'm beautiful and comprehensive!'
我怎样才能做到这一点?
尝试用户repr:
class MyCustomObject(object):
state = "I'm beautiful and comprehensive!"
def __repr__(self):
return "State is: {}".format(self.state.__str__())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句