Dummy object in Python

python

Solution

Try using `Mock` objects. Any call made on a `Mock` object will return another `Mock` object.

For example:

>>> from mock import Mock
>>> test = Mock()
>>> test.doStuff()
<Mock name='mock.doStuff()' id='4373729360'>
>>> test2 = test.doStuff
>>> test2
<Mock name='mock.doStuff' id='4373693712'>
>>> test2()
<Mock name='mock.doStuff()' id='4373729360'>

As shown here, it is consistent - calling `doStuff()` multiple times returns the same `Mock`, and if you call the `Mock` created by `mock.doStuff` it will return the same `Mock` as `doStuff()`.

Mock objects are commonly used in unit tests, so there is a lot more that you can do with them than what I've shown here. Read more here if you are interested.

Problem

Is it possible to create a "stupid" dummy object in Python that will always no-op and never throw an error, no matter how is called or otherwise manipulated? The use case for this is when there's an object that is used to create side effects in the normal case, but if run in a different environment (suppose, during development) should not do anything and fail silently. ``` try: o = Obj() except ImportError: # we're in development mode o = DummyObj() o.doStuff() # should work or fail silently ```

Original source

Related problems