Normally, when using mock, I'll have
from mock import Mock
m = Mock()
m
<Mock id='4334328720'>
Is it possible to change this output?
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.