我正在过渡到Python3,并一直在探索stdlib的一些功能。functools.singledispatch引起了我的注意,我一直在玩它。然而,在我尝试在课堂上使用它的时候,我遇到了一些问题。在
它似乎不适用于类内注册的函数,您可以通过直接调用乐趣。快感(type(arg))(argname=arg),我想知道是否有更好的方法来实现它。在
我尝试在注册的上面和下面使用@classmethod和@staticmethod作为装饰器,但是没用。在
下面是一个人为的例子,它注册处理程序,以便在创建类时转换输入参数,以确保它始终是一个列表。在
from functools import singledispatch
class UrlDispatcher(object):
@singledispatch
def url_input(self, input):
print('input wasn\'t dispatched', input)
@url_input.register(str)
def _(self, input):
print('input is a str', input)
self.input = [input]
@url_input.register(list)
def _(self, input):
print('input is a list', input)
self.input = input
def __init__(self, arg):
# Works, albeit clunkily
self.url_input.dispatch(type(arg))(self,input=arg)
# Always uses the base dispatcher
self.url_input(input=arg)
a = "http://www.cnn.com"
b = ["http://www.google.com", "http://www.slashdot.org"]
s1 = UrlDispatcher(a)
s2 = UrlDispatcher(b)
我找到了答案-你不知道。在
http://code.activestate.com/lists/python-dev/122554/
引用我在上面的URL上找到的一篇文章,我想已经解释过了——简短的回答是“泛型函数”是用于无状态算法的。我不知道这个定义。在
下面的方法应该行得通。不管这是不是最好的解决办法,我不知道。在
相关问题 更多 >
编程相关推荐