Python 使用带有“捕获所有”方法签名的multipledispatch
我最近认识了,它很漂亮。我可以这样做,例如:Python 使用带有“捕获所有”方法签名的multipledispatch,python,python-3.x,Python,Python 3.x,我最近认识了,它很漂亮。我可以这样做,例如: from multipledispatch import dispatch class cls(): @dispatch(object) def __init__(self, obj): #construct a cls instance using obj# @dispatch(int, int) def __init__(self, a, b): #construct a cls
from multipledispatch import dispatch
class cls():
@dispatch(object)
def __init__(self, obj):
#construct a cls instance using obj#
@dispatch(int, int)
def __init__(self, a, b):
#construct a cls instance using x and y#
我想知道multipledispatch.dispatch是否有办法在末尾添加一个方法来捕获任何和所有参数。我已经试着查看了文档,但没有找到一种方法
我想做的是添加一个带有self、*args、**kwargs签名的uuu init_uuu的最终版本,这样我就可以为以前@dispatch修饰过的版本中未考虑的所有其他参数组合定制行为。我尝试过做如下操作,但它仅在没有提供参数时有效:
class cls():
@dispatch(object)
def __init__(self, obj):
#construct a cls instance using obj#
@dispatch()
#this version of __init__only get called when zero arguments supplied
#would like it to be called for all argument combinations not matching those
#accounted for above
def __init__(self, *args, **kwargs):
raise TypeError("cls instance cannot be constructed from supplied arguments.")
我想OP是在谈论。看起来他们仍然在讨论如何支持*ARG。@Kevin谢谢。我想我现在会坚持使用过载模块。