如何在 VSCode 的监视面板中查看类实例的字符串表示而不是其类型?

埃里克·麦克拉克伦

在 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章