1

is there in python something like a decorator for IDE's or debuggers like IDLE or Visual Studio where I can configure which variable should be written in the variable view for a class?

Example code:

idList = []
idList.append("X342")
idList.append(Token("X9999"))

and the variable view in Visual Studio:

https://i.sstatic.net/q9Q0I.png

Instead of the "Tokens.Token object at (...)" i want to specify in the Token class what the debugger should write. In this case the str "X9999".

Can someone help me?

1 Answer 1

2

The debugger almost certainly uses the repr() of the object. You can define your own:

class Token(object):
    def __init__(self, id):
        self.id = id
    def __repr__(self):
        return "<Token %s>" % self.id

If Token is a class from a library, you can subclass it and use the subclass, or if that's not possible, monkey-patch the original class to override its __repr__. The latter looks something like this:

def __repr__(self):
    return "<Token %s>" % self.id   # or wherever `X9999` is stored

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

1 Comment

Thank you! I only tried it with str() but you're right I should use repr().

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.