singledispatch


Гео и язык канала: не указан, не указан
Категория: не указана



Гео и язык канала
не указан, не указан
Категория
не указана
Статистика
Фильтр публикаций


https://docs.python.org/3/library/functools.html#functools.singledispatch

Transform a function into a single-dispatch generic function.

To define a generic function, decorate it with the @singledispatch decorator. Note that the dispatch happens on the type of the first argument, create your function accordingly:

>>> from functools import singledispatch
>>> @singledispatch
... def fun(arg, verbose=False):
... if verbose:
... print("Let me just say,", end=" ")
... print(arg)

To add overloaded implementations to the function, use the register() attribute of the generic function. It is a decorator. For functions annotated with types, the decorator will infer the type of the first argument automatically:

>>> @fun.register
... def _(arg: int, verbose=False):
... if verbose:
... print("Strength in numbers, eh?", end=" ")
... print(arg)
...
>>> @fun.register
... def _(arg: list, verbose=False):
... if verbose:
... print("Enumerate this:")
... for i, elem in enumerate(arg):
... print(i, elem)




Видео недоступно для предпросмотра
Смотреть в Telegram



Показано 4 последних публикаций.

1

подписчиков
Статистика канала