Questions tagged [single-dispatch]

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.

34 questions
2
votes
0 answers

Single dispatch by value methodes…

I am reading some binary data from a serial port. Each packet can contain a wide array (a couple of dozen or so) messages identified by specific byte sequences. To make matter simple, let us assume only one bye so that 0x01 corresponds to the first…
Sardathrion - against SE abuse
  • 17,269
  • 27
  • 101
  • 156
2
votes
1 answer

lru_cache interferes with type checking done by single_dispatch

I have a method dispatch decorator with three registered functions. One dispatches on int, which works fine. The second dispatched on a custom type, also works fine. The third is also a custom type, but the Class is wrapped with the lru_cache…
Adam Michael Wood
  • 1,730
  • 16
  • 23
1
vote
1 answer

Does python functools.singledispatch work with Generator type?

I extended the example at https://docs.python.org/3/library/functools.html#functools.singledispatch by adding a registration for generator type from functools import singledispatch from typing import Generator @singledispatch def fun(arg,…
zyxue
  • 7,904
  • 5
  • 48
  • 74
1
vote
0 answers

Confused about single-dispatch in Java vs multiple dispatch

I’ve read a good post Just still a little confused about the concept of multiple dispatch (not in Java) vs single dispatch (in Java). Let’s use this example: class A { m(A a) { System.out.println(“In A: “ + a.getClass()); } } class B…
ppp
  • 111
  • 7
1
vote
1 answer

Pythonic(OO) way to choose class method depending on the type of object

Is there any better Pythonic / Object Oriented way to choose which particular class method is to be executed at run time, depending on the type of the object, since using the type() method is not considered elegant(?) I wrote the following code for…
Dost Arora
  • 355
  • 3
  • 12
1
vote
0 answers

Examples of functools library not reproducilble

I am studying the functools library in python. However, when I copy the examples from the documentation for the decorator @singledispatch I do not get the same results as the ones that are reported on the docs. from functools import…
1
vote
1 answer

Travis pylint build failing with error regarding singledispatch

My Travis build is currently failing at pylint with the error: Traceback (most recent call last): File "/home/travis/virtualenv/python3.3.6/bin/pylint", line 11, in sys.exit(run_pylint()) File…
Daniel O'Brien
  • 345
  • 4
  • 11
0
votes
1 answer

PyTest for @singledispatch and @typechecked not raising expected error

Goal: Successfully pass test cases, for NotImplementedError in test_score_not_implemented_error(). The purpose of @singledispatch def score() is to raise NotImplementedError, if when the provided arguments for count_neg and count_pos do not match…
DanielBell99
  • 896
  • 5
  • 25
  • 57
0
votes
1 answer

how to use Python multimethod with custom class as argument type

I have the following classes setup: class A: pass class B(A): pass class C(A): pass Then I have a method that gets called with 2 arguments: x -> always a string y -> one of the classes above (A, B, or C) And we do something different…
KSS
  • 147
  • 1
  • 8
0
votes
0 answers

Properties with single dispatch

Is it possible to compose a property with singledispatch / singledispatchmethod features? I have tried the obvious patterns (nesting @singledispatchmethod with @prop.setter, etc) and get various errors. Here's a MWE of what I'd like to do, which…
jay
  • 493
  • 1
  • 3
  • 11
0
votes
0 answers

python singledispatch with several arguments

for example: @singledispatch def f( a: str, …
Vince
  • 3,979
  • 10
  • 41
  • 69
0
votes
1 answer

How to overload an __eq__ function that gets an object of the self class

What i'm trying is to overload this function that is in Posicion class using singledispatch and trying to follow the OOP: def __eq__(self, other): if isinstance(other, Posicion): return other.get_posicion() == self.get_posicion() …
R14
  • 15
  • 4
0
votes
1 answer

Do any generic functions in the base library dispatch on matrices?

I'm having some trouble writing some code that dispatches on matrices. To assist me, I'd like to see what generic functions in the base library dispatch on matrices. Is there any way to get R to give me a list of them? Failing that, does anyone know…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
0
votes
1 answer

Using singledispatch with custom class(CPython 3.8.2)

Let's say I want to set functions for each classes in module Named 'MacroMethods'. So I've set up singledispatch after seeing it in 'Fluent Python' like this: @singledispatch def addMethod(self, obj): print(f'Wrong Object {str(obj)} supplied.') …
jupiterbjy
  • 2,882
  • 1
  • 10
  • 28
0
votes
0 answers

python: multiple constructors(__init__ signatures) in range class, how is it possible?

I'm aware that multiple constructors in python is not possible. However, I found the range class has two __init__ signatures from the official docs. class range(stop) class range(start, stop[, step]) At first I thought it's implemented like…