Skip to main content

Pattern matching for python

Project description

Pattern matching for python. Works in Python 2.7, 3.3+ and pypy.


pip install patterns


from patterns import patterns, Mismatch

def factorial():
    if 0: 1
    if n is int: n * factorial(n-1)
    if []: []
    if [x] + xs: [factorial(x)] + factorial(xs)
    if {'n': n, 'f': f}: f(factorial(n))

assert factorial(0) == 1
assert factorial(5) == 120
assert factorial([3,4,2]) == [6, 24, 2]
assert factorial({'n': [5, 1], 'f': sum}) == 121

factorial('hello') # raises Mismatch

See tests for more examples.


  • docs
  • aliases for structures
  • destructure objects
  • name parameter
  • better handling of Mismatch passing to function env
  • non-strict dict matching

Download files

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

Files for patterns, version 0.3
Filename, size File type Python version Upload date Hashes
Filename, size patterns-0.3.tar.gz (6.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page