python overloaded achieved (single-dispatch generic function)

DAY 11. python reload

Function overloading means defined number of parameters allows different types or functions of the same name will be selected according to the function to call the type of the passed parameters at run time
, but by default, Python does not support function overloading, the same name is defined coverage function occurs

def foo(a:int):
    print(f'int {a}')

def foo(b:list):
    print(f'list{b}')

foo(3)
foo([i for i in range(3)])

# list3
# list[0, 1, 2]

For reasons that are not supported, I think it probably is not necessary, first of all, function overloading may occur only in two cases, one different parameter types, the second is the number of different parameters, the first case, there is a type of duck making function parameters do not care about the type of care only about the behavior of parameters, so you can pass any type of argument, the second case, the default parameter makes it possible to pass any number of parameters, so it is overloaded function in python very sad, but if you have to implement function overloading, can be implemented using the overload forwarding mechanisms, i.e., increased 3.4 single generic function dispatch (single-dispatch generic function)

11.2 single generic function dispatch

  • Generic function generic function: function consists of many functions, it can decide to call that function according to different parameter types
  • Single dispatch, single-dispatch: one kind of generic function dispatch form, implemented according to the type wherein a single parameter selected.

Therefore, a single generic function dispatch function is a generic function which is used in accordance with the first parameter determined by the type of function

The function declaration of a generic function is modified may be used @singledispatch, from the need to functoolsimport module, singledispatch There are two common methods, register and dispatch

from functools import singledispatch

@singledispatch
def Foo(arg,*args):
    print(arg)

This implements a generic function, his assigned place on the first parameter type, if you want to achieve this based on overloaded method requires the use of his register,

from functools import singledispatch

@singledispatch
def Foo(arg,*args):
    print(arg)

# 使用了类型注释
@Foo.register
def _1(arg:int,*args):
    print(f'int - {arg}')

# 没有使用类型注释,显式传递给修饰器
@Foo.register(list)
def _2(arg,*args):
    print(f'list - {arg}')

if __name__ == '__main__':
    Foo(3)  # int - 3
    Foo([i for i in range(10)])  # list - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For variable type annotation, singledispatch automatically infer the type of the first parameter, such that the variable is not used for the above int type annotation, to the delivery type can be explicit singledispatch, as the following list of

register attributes can also be in the form of a function call, which can be used in the expression lambdas, such as

>>> def nothing(arg, verbose=False):
...     print("Nothing.")
...
>>> fun.register(type(None), nothing)

If not registered for a particular type of realization, it will use the modified function was @singledispatch

Foo("string")  # string

To check the generic function which implementation will choose a given type, use the dispatch () property

print(Foo.dispatch(int))  # <function _1 at 0x000001D7C2724B70>
print(Foo.dispatch(list))  # <function _2 at 0x000001D7C2792E18>
print(Foo.dispatch(str))  # <function Foo at 0x000001FB456FC268>

To access all implementations registered, please use the read-only attribute of the registry

11.3 summary

python does not support overloaded default, but can be achieved using a single generic function dispatch, declare a generic function requires decorator @singledispatch, it has three attributes, register to register for a specific type of "overloaded function" must be specified here which is aimed at a specific type, you can type the first parameter annotation, can also pass an explicit type to register, otherwise it will throw a TypeError exception; dispatch function to view a specific property type to be selected; registry to achieve access to all registered.

Reference links:

functools.singledispatch

single dispatch

generic function

python Overloaded

Why Python does not support function overloading? Most of the other functions are supported?

Published 62 original articles · won praise 33 · views 10000 +

Guess you like

Origin blog.csdn.net/zjbyough/article/details/97152241