0

for example:

@singledispatch                                                                                      
def f(                                                                                               
        a: str,                                                                                      
        b: list | dict                                                                               
)->None:                                                                                             
    ...                                                                                              
                                                                                                     
@f.register                                                                                          
def flist(                                                                                           
        a: str,                                                                                      
        b: list                                                                                      
)->None:                                                                                             
    print("flist:",type(b))                                                                          
                                                                                                     
@f.register                                                                                          
def fdict(                                                                                           
        a: str,                                                                                      
        b: dict                                                                                      
)->None:                                                                                             
    print("fdict:",type(b))                                                                          
                                                                                                     
                                                                                                     
a = "---"                                                                                           
                                                                                                     
b = [1,2]                                                                                            
f(a,b)                                                                                               
                                                                                                     
b = {1:2}                                                                                            
f(a,b)   

My (apparently incorrect) understanding, is that this should print (I hope for obvious reasons):

flist: <class 'list'>
fdict: <class 'dict'>

but in fact, this prints:

fdict: <class 'list'>
fdict: <class 'dict'>

why does the first call to f redirect to 'fdict', despite 'b' being a list ?

ringo
  • 978
  • 7
  • 16
Vince
  • 3,979
  • 10
  • 41
  • 69
  • 3
    Single dispatch dispatches on the type of the first argument, so both of your functions are for the same type (both have `str`-type first arguments). I'm going to guess that it picks the last registered when you have multiple dispatch functions dispatching on the same type. `b` needs to be the first argument to dispatch on its type. – Carcigenicate Mar 12 '23 at 18:09
  • Yes; `f.register` simply [replaces the previous function for `str`](https://github.com/python/cpython/blob/3.11/Lib/functools.py#L898) without regard to what may have been previously registered. – chepner Mar 12 '23 at 18:13
  • just gave it a try, and indeed. – Vince Mar 12 '23 at 18:13

0 Answers0