Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

function can be dispatched by its arguments

Project description

arg_dispatch

function can be dispatched by its arguments

Example

from arg_dispatch import dispatch


# Functions
@dispatch
def demo(a, b):
    return 'hello'
    
@dispatch
def demo(c):
    return 'world'
    

demo(a=1, b=2)  # return 'hello'
demo(c=3)       # return 'world'

# try to call a function which has not been registed
demo(d=4)       # raise `FunctionNotRegist`

# Methods
class Demo(object):
    @dispatch
    def demo(self, a, b):
        return 'hello'
        
    @dispatch
    def demo(self, c):
        return 'world'
        
instance = Demo()
instance.demo(a=1, b=2)  # return 'hello'
instance.demo(c=3)       # return 'world'

# try to call a method which has not been registed
instance.demo(d=4)       # raise `FunctionNotRegist`

Notice💣

positional arguments must be required

demo(1, 2)          # Boom!💣, raise `ArgumentError`
instance.demo(1, 2) # Boom!💣, raise `ArgumentError`

default value is also not supported

@dispatch
def demo(a, b=1):            # Boom!💣, raise `ExistDefaultValue`
    return 'hello'
    
class Demo(object):
    @dispatch
    def demo(self, a, b=1):  # Boom!💣, raise `ExistDefaultValue`
        return 'hello'

Project details


Download files

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

Files for arg-dispatch, version 0.1.3
Filename, size File type Python version Upload date Hashes
Filename, size arg_dispatch-0.1.3-py3-none-any.whl (15.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size arg_dispatch-0.1.3.tar.gz (14.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page