Python:创建一个在任何函数调用中都不返回任何值的类
我想知道是否有可能创建一个无论调用什么“方法”都始终不返回任何值的类 比如说,Python:创建一个在任何函数调用中都不返回任何值的类,python,function,class,Python,Function,Class,我想知道是否有可能创建一个无论调用什么“方法”都始终不返回任何值的类 比如说, # The following all returns None Myclass.method1() Myclass.method2(1, 2, 3) Myclass.method2(1,2) 基本上,我想实现一个类,这样 任何未由类定义的非内置方法都将被接受并识别为有效方法 来自第1点的所有方法都将不返回任何值 我知道,mock.MagicMock可以给我这个结果,但是速度非常慢,所以我想知道是否有更好的方法来
# The following all returns None
Myclass.method1()
Myclass.method2(1, 2, 3)
Myclass.method2(1,2)
基本上,我想实现一个类,这样
mock.MagicMock
可以给我这个结果,但是速度非常慢,所以我想知道是否有更好的方法来实现这个结果。是的,很简单
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
亲爱的,正是我需要的。谢谢