4

What function (if any) should I implement so that VScode (or really python) can pick up the value of a user defined object in the VScode variable pane whilst debugging?

In this image you can see that arr shows up as [1, 2, 3, 4]. How can I get the variable node to show its value (1) instead of <__main__.ListNode object at 0x7f4a19eafb20>

1 Answer 1

3

Since node is an object, its corresponding __repr__ method is returning that value by default. If you want to see the actual node value instead, then you can add a __repr__ method to ListNode class.

VScode uses the __repr__ method when showing the variable values. Here's a sample ListNode class with __repr__ method added -

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

    def __repr__(self):
        return str(self.val)


node = ListNode(1)
print(node)

If you run this in VScode debug mode you would see the expected node value instead of <__main__.ListNode object at 0x101bed2b0>

Without __repr__ method - enter image description here

With __repr__ method - enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

what to do if I want to walk the List through the next pointer/handle in the debugger pane
@rioV8, you can still do that same as before, by expanding the tree under the node. You would see next object under node which would show node linked to next of current node object.
I tried and you are right, the __repr__ only modifies the collapsed-text/object-title shown in the debugger, it has no influence on the traversal of objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.