A language feature which allows different versions of a function to be called depending on the type of one argument. If decided by the types of multiple objects, it is multiple-dispatch.
Questions tagged [single-dispatch]
34 questions
59
votes
3 answers
How can I use functools.singledispatch with instance methods?
Python 3.4 added the ability to define function overloading with static methods. This is essentially the example from the documentation:
from functools import singledispatch
class TestClass(object):
@singledispatch
def test_method(arg,…

Dustin Oprea
- 9,673
- 13
- 65
- 105
32
votes
8 answers
Is C# a single dispatch or multiple dispatch language?
I'm trying to understand what single and multiple dispatch are, exactly.
I just read this:
http://en.wikipedia.org/wiki/Multiple_dispatch
And from that definition is seems to me that C# and VB.Net are multiple-dispatch, even though the choice of…

Daniel Magliola
- 30,898
- 61
- 164
- 243
23
votes
2 answers
How to create a "single dispatch, object-oriented Class" in julia that behaves like a standard Java Class with public / private fields and methods
I read in a book that "you can't create traditional 'classes' in julia with single-dispatch-style methods like obj.myfunc()" ... and I thought that sounded more like a challenge than a fact.
So here's my JavaClass type with public / private fields…

Tasos Papastylianou
- 21,371
- 2
- 28
- 57
7
votes
9 answers
What is - Single and Multiple Dispatch (in relation to .NET)?
Is it the same as overloading, if not, can you please provide and example of each in C#
I have read the responses to a similar question asked in SO ... i did not understand the responses posted to it.
Similar question asked here
EDIT: With the new…

Developer
- 17,809
- 26
- 66
- 92
7
votes
1 answer
Unregister for singledispatch?
Is there way to "unregister" a registered function for a generic ?
For example:
from functools import singledispatch
@singledispatch
def foo(x):
return 'default function'
foo.register(int, lambda x: 'function for int')
# later I would like to…

lgautier
- 11,363
- 29
- 42
6
votes
1 answer
Type hints and @singledispatch: how do I include `Union[...]` in an extensible way?
I'm refactoring a function that converts a variety of date formats (ie. ISO 8601 string, datetime.date, datetime.datetime, etc) to a Unix timestamp.
I want the new function to use @singledispatch instead of type inspection, but I can't figure out…

tenni
- 475
- 3
- 11
5
votes
1 answer
Fast single dispatch to get around multiple dispatch at runtime
When type inference falters (::Any in @code_warntype printout), my understanding is that function calls are dynamically dispatched. In other words, at run-time, the arguments' types are checked to find the specialization (MethodInstance) for the…

BatWannaBe
- 4,330
- 1
- 14
- 23
4
votes
1 answer
functools.singledispatchmethod with own class as arg type
I would like to use functools.singledispatchmethod to overload the binary arithmetic operator methods of a class called Polynomial. The problem I have is that I can't find a way to register method calls where other is a Polynomial.
Perhaps better…

Vinzent
- 1,070
- 1
- 9
- 14
3
votes
1 answer
How to combine @singledispatch and @lru_cache?
I have a Python single-dispatch generic function like this:
@singledispatch
def cluster(documents, n_clusters=8, min_docs=None, depth=2):
...
It is overloaded like this:
@cluster.register(QuerySet)
@lru_cache(maxsize=512)
def _(documents, *args,…

Carsten
- 1,912
- 1
- 28
- 55
3
votes
2 answers
How can I dispatch Python 2 functions based on the data type passed to the function?
I would like to dispatch Python functions dependent (e.g. using a dict approach) on the data type of the argument passed to the "dispatching" function (e.g. using isinstance()). Are there implementation alternatives? What's the easiest approach?

thinwybk
- 4,193
- 2
- 40
- 76
2
votes
1 answer
AttributeError: 'function' object has no attribute 'register' when using functools.singledispatch
Goal: create a single-dispatch generic function; as per functools documentation.
I want to use my_func() to calculate dtypes: int or list, in pairs.
Note: I've chosen to implement type hints and to raise errors for my own test cases, outside of the…

DanielBell99
- 896
- 5
- 25
- 57
2
votes
1 answer
Abstract method with single-dispatch generic functions
I want to put single-dispatch method in functools module with abstract method in abc module in one method in my abstract class and then using the method in my subclasses
based on first argument type. but the problem is the dispatch method doesn't…

amirhossein
- 27
- 8
2
votes
0 answers
singledispatchmethod using class it is used in?
For practice I am trying to build a class for 2d vectors, I want to override the multiplication operator to be able to multiply a vector by a scalar, but also by another vector (dot product).
This single argument based polymorphism is normally…

Ozz
- 79
- 6
2
votes
0 answers
Making wrapper objects compatible with @singledispatch?
Say I have a class like this:
class Wrapper(object):
def __init__(self, obj):
self.__obj = obj
def __getattr__(self, name):
logger.debug('Accessing %s', name)
return getattr(self.__obj, name)
Elsewhere in the…

Dan Passaro
- 4,211
- 2
- 29
- 33
2
votes
1 answer
Singledispatch decorator doesn't work as advertised
I am testing python's singledispatch: https://docs.python.org/3/library/functools.html?highlight=singledispatch#functools.singledispatch
Block A is supposed to work as well as Block B according to the document. However, you can see in the output…

Jeremy Chen
- 1,299
- 2
- 11
- 18