Settings

Theme

Plum: Multiple Dispatch in Python

github.com

4 points by fhchl 2 years ago · 2 comments

Reader

cameron_b 2 years ago

A quick implementation of the examples and re-implementation of the same process with match-case pattern and type() typechecking shows that the overhead this introduces is quite costly.

I understand that their examples are trivial, and the project's parent is a more robust type-checking function, but what real world cases would benefit from this @dispatch decorator? My first guess was that it might do multiple-matching but that seems to be not the case ("it picks the right one") so I can't make Fizz-Buzz with it. Is this a case of a branch of an earlier Pythonic style being superseded by new python built-ins?

references:

plum dispatch

real 0m0.078s user 0m0.066s sys 0m0.013s

match-case

real 0m0.015s user 0m0.008s sys 0m0.007s

  ===
  def f(x):
     match type(x):
        case 'str':
           return 'string'
        case 'int':
           return 'integer'


  ===
  from plum import dispatch

  @dispatch
  def f(x: str):
     return "string"

  @dispatch
  def f(x: int):
     return "integer"

  ===
  test = [1, 7, '5', 'bob', 1564813548, "Four score and seven years ago",
        456, '879', "I can't understand why my fingers messed that up",
        852, 1234567890, "More of the same", "Spam spam spam", 1, 1, 2,
        3, 5, 8, 13, 21, 33, 54, 87, "Fizz", "Buzz"]

  for one in test:
     f(one)
fhchlOP 2 years ago

This lookes well made and based on the great beartype. I wonder if there are packages that use this and if one could imagine a similarity interfunctioning ecosystem for Python as Julia seems to have.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection