Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Overload python 2 functions with decorator.

Project description

Overload python functions to behave diffrently depending on number and types of arguments.


pip install p2-overload


from overload import *

# types and number or agrs:
def quantify(n):
    return 'quantify' * n

def quantify(s):
    return s * 2

@overload(str, int)
def quantify(s, n):
    return s * n

assert quantify(2) == 'quantifyquantify'
assert quantify('bye') == 'byebye'
assert quantify('xy', 3) == 'xyxyxy'

# predicates on args
@overload(int, int)
def step(start, step):
    return start + step

@overload(int, callable)
def step(start, succ):
    return succ(start)

# use tuple of types
@overload((list, tuple))
def twice(coll):
    return coll + coll

# use custom predicates
from whatever import _

@overload(_ < 2) # same as lambda x: x < 2
def fib(n):
    return n

def fib(n):
    return fib(n-1) + fib(n-2)


  • support named and optional arguments
  • ellipsis

Project details

Release history Release notifications | RSS feed

This version


Download files

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

Files for p2-overload, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size p2-overload-0.1.tar.gz (3.0 kB) File type Source Python version None Upload date Hashes View

Supported by

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