如何根据传递给函数的数据类型分派Python 2函数?
我希望根据传递给“dispatching”函数的参数的数据类型(例如,使用如何根据传递给函数的数据类型分派Python 2函数?,python,python-2.7,generic-function,single-dispatch,Python,Python 2.7,Generic Function,Single Dispatch,我希望根据传递给“dispatching”函数的参数的数据类型(例如,使用isinstance())来分派Python函数(例如,使用a)。是否有其他实施方案?最简单的方法是什么?从Python 3.4开始,Python标准库包括对的支持 这允许您注册多个函数来处理不同的类型,并且它将根据类型处理分派,包括子类测试和缓存。中描述了该方法 有一个支持Python2.6及以上版本的脚本,由PEP作者编写 请注意,Python2.7将很快进入最后一个生命周期结束日期,届时它将不再收到错误修复和安全更新
isinstance()
)来分派Python函数(例如,使用a)。是否有其他实施方案?最简单的方法是什么?从Python 3.4开始,Python标准库包括对的支持
这允许您注册多个函数来处理不同的类型,并且它将根据类型处理分派,包括子类测试和缓存。中描述了该方法
有一个支持Python2.6及以上版本的脚本,由PEP作者编写
请注意,Python2.7将很快进入最后一个生命周期结束日期,届时它将不再收到错误修复和安全更新;您确实需要尽早计划升级到Python 3。当您这样做时,您会注意到Python3.7版本支持使用类型暗示来记录每个函数接受的类型
例如,从嵌套字典和列表数据结构(典型的JSON数据结构)中删除None
和False
值的一系列函数可以定义为:
from functools import singledispatch
@singledispatch
def remove_null_false(ob):
return ob
@remove_null_false.register
def _process_list(ob: list):
return [remove_null_false(v) for v in ob]
@remove_null_false.register
def _process_list(ob: dict):
return {k: remove_null_false(v) for k, v in ob.items()
if v is not None and v is not True and v is not False}
在Python版本<3.7中,您必须将类型移动到
@remove\u null\u false.register(…)
装饰器工厂符号。从Python 3.4开始,Python标准库包括对的支持
这允许您注册多个函数来处理不同的类型,并且它将根据类型处理分派,包括子类测试和缓存。中描述了该方法
有一个支持Python2.6及以上版本的脚本,由PEP作者编写
请注意,Python2.7将很快进入最后一个生命周期结束日期,届时它将不再收到错误修复和安全更新;您确实需要尽早计划升级到Python 3。当您这样做时,您会注意到Python3.7版本支持使用类型暗示来记录每个函数接受的类型
例如,从嵌套字典和列表数据结构(典型的JSON数据结构)中删除None
和False
值的一系列函数可以定义为:
from functools import singledispatch
@singledispatch
def remove_null_false(ob):
return ob
@remove_null_false.register
def _process_list(ob: list):
return [remove_null_false(v) for v in ob]
@remove_null_false.register
def _process_list(ob: dict):
return {k: remove_null_false(v) for k, v in ob.items()
if v is not None and v is not True and v is not False}
在Python版本<3.7中,您必须将类型移动到
@remove\u null\u false.register(…)
装饰器工厂符号。请查看下面的示例
def get_int_square(a):
"""
Returns square of integer parameter
"""
return a ** 2
def get_float_cube(a):
"""
Returns cube of float parameter
"""
return a ** 3
def sum_of_items(l):
"""
Returns sum of all the items in list
"""
return sum(l)
def get_squared_items(t):
return tuple(item ** 2 for item in t)
def dispatching(a):
"""
Calls the corresponding functions based on match found in the dictionary
"""
functions = {
'int': get_int_square,
'float': get_float_cube,
'list': sum_of_items,
'tuple': get_squared_items
}
data_type = str(type(a)).split("'")[1]
result = functions[data_type](a)
return result
if __name__ == "__main__":
print(dispatching(12)) # 144
print(dispatching(1.2)) # 1.7279999999999998
print(dispatching((4, 7, 9, 3, 1, 5, 8))) # (16, 49, 81, 9, 1, 25, 64)
print(dispatching([56, 4, 50, 26, 24])) # 160
请看下面的例子
def get_int_square(a):
"""
Returns square of integer parameter
"""
return a ** 2
def get_float_cube(a):
"""
Returns cube of float parameter
"""
return a ** 3
def sum_of_items(l):
"""
Returns sum of all the items in list
"""
return sum(l)
def get_squared_items(t):
return tuple(item ** 2 for item in t)
def dispatching(a):
"""
Calls the corresponding functions based on match found in the dictionary
"""
functions = {
'int': get_int_square,
'float': get_float_cube,
'list': sum_of_items,
'tuple': get_squared_items
}
data_type = str(type(a)).split("'")[1]
result = functions[data_type](a)
return result
if __name__ == "__main__":
print(dispatching(12)) # 144
print(dispatching(1.2)) # 1.7279999999999998
print(dispatching((4, 7, 9, 3, 1, 5, 8))) # (16, 49, 81, 9, 1, 25, 64)
print(dispatching([56, 4, 50, 26, 24])) # 160
你试过使用字典法吗?发生了什么?我强烈要求你考虑升级到Python 3,在这个时候标准库为你提供了基于类型的调度。但是我必须在这里显式地使用python-2.7。您尝试过使用字典方法吗?发生了什么?我强烈要求你考虑升级到Python 3,在这个时候标准库为你提供了基于类型的调度。但是我必须在这里明确地使用python-2.7。使用自己的类型而不是int、float等应该不是问题。凉的你也可以使用其他类型,你只需要添加更多的代码行。如果您有任何问题,请发表评论。感谢您的回复。使用自己的类型而不是int、float等应该不是问题。凉的你也可以使用其他类型,你只需要添加更多的代码行。如果您有任何问题,请发表评论。谢谢你的回复。