New features in Python 3.4.0 (3)-Single-dispatch generic functions

Today is Single-dispatch generic functions (PEP-443). Generic functions are often translated as generic functions, but they are used slightly differently depending on the language. In Python, it seems to be used to mean "give the same name to multiple functions that behave differently depending on the argument type".

I don't understand at all even if I write, so I'll give you an example. Consider a function whose output differs depending on the argument type. It looks like this without using generic functions.

def print_func(arg):
    arg_type = type(arg)
    if arg_type is int:
        print("found integer:", arg)
    elif arg_type is list:
        print("found list:", arg)
    else:
        print("found something:", arg)

print_func(1)
print_func(['a','b','c'])
print_func(1.23)

Process while checking the type of the input argument one by one with the if statement. The execution result is as follows.

found integer: 1
found list: ['a', 'b', 'c']
found something: 1.23

If you write this with Single-dispatch generic functions, it will be like this.

from functools import singledispatch

@singledispatch
def print_func(arg):
    print("found something:", arg)

@print_func.register(int)
def _(arg):
    print("found integer:", arg)

@print_func.register(list)
def _(arg):
    print("found list:", arg)

print_func(1)
print_func(['a','b','c'])
print_func(1.23)

It feels like modifying the base function with a single ispatch decorator and adding processing for each type to it using the register method. Which one is better is subtle, but if the processing for each type becomes longer, this may be cleaner than putting it in one if statement.

The result is the same as above, so it is omitted.

But I can't think of any use. It is common to see a certain value and dispatch it, for example, parsing of such a data group because the format of the data that follows can be known from the value of Header. In such a case, it is often realized by preparing a Dict that subtracts a function from the value, but this is a type.

Recommended Posts

New features in Python 3.4.0 (3)-Single-dispatch generic functions
What's new in Python 3.5
New in Python 3.4.0 (1)-pathlib
What's new in Python 3.6
What's new in Python 3.10 (Summary)
Overriding library functions in Python
What's new in Python 3.4.0 (2) --enum
What's new in Python 3.9 (Summary)
Python functions learned in chemoinformatics
[python] Manage functions in a list
What's new in python3.9 Merge dictionaries
Using global variables in python functions
Dynamically define functions (methods) in Python
New Python grammar and features not mentioned in the introductory book
Python functions
New features in Python 3.9 (1)-Union operators can be used in dictionary types
New in Python 3.9 (2)-Sort directed acyclic graphs in Python
[Python3] Dynamically define global variables in functions
Easily use your own functions in Python
Scene recognition by GIST features in Python
[Tips] Easy-to-read writing when connecting functions in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
A note on optimizing blackbox functions in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Practice applying functions and global variables in Python
Constant in python
Use various rabbimq features with pika in python
I touched some of the new features of Python 3.8 ①
#Python basics (functions)
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
[Beginner] Python functions
StepAIC in Python
Create a new page in confluence with Python
Get files, functions, line numbers running in python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Python Easy-to-use functions