Skip to main content

Python Pattern Matching

Project description

Python Pattern Matching

Python, I love you. But I'd like you to change. It's not you, it's me. Really.
See, you don't have pattern matching. But, that's not the root of it. Macros
are the root of it. You don't have macros but that's OK. Right now, I want
pattern matching. I know you offer me ``if``/``elif``/``else`` statements but I
need more. I'm going to abuse your functions. Guido, et al, I hope you can
forgive me. This will only hurt a little.

`Python Pattern Matching`_ is an Apache2 licensed Python module for `pattern
matching`_ like that found in functional programming languages. Most projects
that address Python pattern matching focus on syntax and simple cases. Operator
overloading is often used to change the semantics of operators to support
pattern matching. In other cases, function decorators are used to implement
multiple dispatch, sometimes known as function overloading. Each of these
syntaxes, operators, and decorators, is really more of a detail in the
application of pattern matching.

A lot of people have tried to make this work before. Somehow it didn't take. I
should probably call this yet-another-python-pattern-matching-module but
"yappmm" doesn't roll off the tongue. Other people have tried overloading
operators and changing codecs. This module started as a codec hack but those are
hard because they need an ecosystem of emacs-modes, vim-modes and the like to
really be convenient.

Python Pattern Matching focuses instead on the semantics of pattern matching in
Python. The dynamic duck-typing behavior in Python is distinct from the tagged
unions found in functional programming languages. Rather than trying to emulate
the behavior of functional pattern matching, this project attempts to implement
pattern matching that looks and feels native to Python. In doing so the
traditional function call is used as syntax. There are no import hooks, no
codecs, no AST transforms.

.. todo::

Python ``match`` function example.

Finally, pythonic pattern matching! If you've experienced the feature before in
"functional" languages like Erlang, Haskell, Clojure, F#, OCaml, etc. then you
can guess at the semantics.

.. todo::

Show the same code without ``patternmatching``.


- Pure-Python
- Developed on Python 3.7
- Tested on CPython 3.4, 3.5, 3.6, 3.7 and PyPy3

.. todo::

- Fully Documented
- 100% test coverage
- Hours of stress testing

.. image::

.. image::


Installing `Python Pattern Matching`_ is simple with `pip

$ pip install patternmatching

You can access documentation in the interpreter with Python's built-in `help`
function. The `help` works on modules, classes, and functions in `pattern

.. code-block:: python

>>> from pypatt import match, bind, bound, like
>>> help(match)

Alternative Packages

- module import, but similar design
- decorator with funky syntax
- Shared at Python Brazil 2013
- operator overloading
- multi-methods
- multi-methods
- the original multi-methods
- multi-methods supporting callables
- not sure how it works but the syntax leaves a lot to be desired
- multi-dispatch with string parsing
- multi-dispatch
- It's own language
- generic multi-dispatch style for business rules
- Pattern-object idea (no binding)
- multi-dispatch style

Other Languages

- F#
- Rust
- Haskell
- Erlang
- Ocaml

Developer Guide

* `Python Pattern Matching Tutorial`_
* `Python Pattern Matching Reference`_
* `Python Pattern Matching Search`_
* `Python Pattern Matching Index`_

.. _`Python Pattern Matching Tutorial`:
.. _`Python Pattern Matching Reference`:
.. _`Python Pattern Matching Search`:
.. _`Python Pattern Matching Index`:

Project Links

* `Python Pattern Matching`_
* `Python Pattern Matching at PyPI`_
* `Python Pattern Matching at GitHub`_
* `Python Pattern Matching Issue Tracker`_

.. _`Python Pattern Matching`:
.. _`Python Pattern Matching at PyPI`:
.. _`Python Pattern Matching at GitHub`:
.. _`Python Pattern Matching Issue Tracker`:

Python Pattern Matching License

Copyright 2015-2019, Grant Jenks

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

.. _`pattern matching`:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patternmatching-3.0.1.tar.gz (12.8 kB view hashes)

Uploaded Source

Built Distribution

patternmatching-3.0.1-py2.py3-none-any.whl (10.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page