3

Normally, when using mock, I'll have

from mock import Mock

m = Mock()
m
<Mock id='4334328720'>

Is it possible to change this output?

1 Answer 1

6

Sure. You can inherit from Mock and change the __repr__ method:

from mock import Mock
class Mock2(Mock):
    def __repr__(self):
        return "Hello World!"

m = Mock2()

>> m
Hello World!

You could also dynamically change the __repr__ method of your object like this:

from mock import Mock
m = Mock()

def new_repr(self):
    return "Hello dynamic Python!"
m.__repr__ = new_repr

>> m
Hello dynamic Python!
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.