nip.utils.runtime_module.overload

nip.utils.runtime_module.overload#

class nip.utils.runtime_module.overload[source]#

Simple function overloading in Python.

Methods Summary

argc([argc])

Overloads a function based on the specified argument count.

args(*argtypes, **kw)

Overload a function based on the specified argument types.

Methods

classmethod argc(argc=None)[source]#

Overloads a function based on the specified argument count.

Parameters:

argc – The argument count. Defaults to None. If None is given, automatically compute the argument count from the given function.

Note

Keyword argument counts are NOT checked! In addition, when the argument count is automatically calculated, the keyword argument count is also ignored!

Example:

@overload.argc()
def func(a):
    print 'Function 1 called'

@overload.argc()
def func(a, b):
    print 'Function 2 called'

func(1) # Calls first function
func(1, 2) # Calls second function
func() # Raises error
classmethod args(*argtypes, **kw)[source]#

Overload a function based on the specified argument types.

Parameters:
  • argtypes – The argument types. If None is given, get the argument types from the function annotations(Python 3 only)

  • kw – Can only contain 1 argument, is_cls. If True, the function is assumed to be part of a class.

Example:

@overload.args(str)
def func(s):
    print 'Got string'

@overload.args(int, str)
def func(i, s):
    print 'Got int and string'

@overload.args()
def func(i:int): # A function annotation example
    print 'Got int'

func('s')
func(1)
func(1, 's')
func(True) # Raises error