# Implementing multimethods in Python

DevFeed: [Implementing multimethods in Python](<https://devfeed.tech/articles/implementing-multimethods-in-python-32133.md>)

Original publisher: [Read original article](<https://adambard.com/blog/implementing-multimethods-in-python/>)

Published: 2014-12-11T00:00:00Z

Content type: tutorial

Language: en

Sources: [Adam Bard](<https://devfeed.tech/sources/adam-bard.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Clojure](<https://devfeed.tech/topics/clojure.md>), [implementation](<https://devfeed.tech/topics/implementation.md>), [function](<https://devfeed.tech/topics/function.md>)

Tags: [clojure](<https://devfeed.tech/tags/clojure.md>), [code](<https://devfeed.tech/tags/code.md>), [function](<https://devfeed.tech/tags/function.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [implementing](<https://devfeed.tech/tags/implementing.md>), [library](<https://devfeed.tech/tags/library.md>), [monkey-patching](<https://devfeed.tech/tags/monkey-patching.md>), [production](<https://devfeed.tech/tags/production.md>), [python](<https://devfeed.tech/tags/python.md>), [test](<https://devfeed.tech/tags/test.md>), [use-cases](<https://devfeed.tech/tags/use-cases.md>)

## AI overview

This article explains multimethods in Python as an implementation of multiple dispatch. It contrasts user-defined dispatch on arbitrary values with conventional single dispatch based on an argument's type, and discusses use cases such as reducing nested conditionals, extending existing classes, and dispatching on dictionary keys.

## Source excerpt

In Clojure (and many other languages), a multimethod is an implementation of multiple dispatch as an alternative to single dispatch. Traditionally, if you define several methods with the same name on different classes, the type/class of the first argument (in Python, self, in many other languages implicit) is used to pick which method to call. This is called "single dispatch" because the decision of which method to call is left up to the inferred type of a single argument. Multimethods take the approach of leaving the dispatch up to the user; you can dispatch on any value at all. You just need to supply a function that returns the value on which you wish to dispatch, and a method for each possible value. For certain cases, this is a lot more flexible than single dispatch.