I was wondering if it is possible to make a class that, always return None no matter what "methods" are called.
For example,
# The following all returns None
Myclass.method1()
Myclass.method2(1, 2, 3)
Myclass.method2(1,2)
Basically, I want to implement a class, such that
I know that mock.MagicMock can give me this result, but it is very slow, so I was wondering if theres a better way to do this.
Yeah, easy.
def return_none(*args, **kwargs):
"""Ignores all arguments and returns None."""
return None
class MyClass(object):
def __getattr__(self, attrname):
"""Handles lookups of attributes that aren't found through the normal lookup."""
return return_none
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With