Skip to main content

simple provision of method overloading

Project description

Simple overloading of functions through an @overload decorator.

This module allows one to provide multiple interfaces for a functions, methods, classmethods, staticmethods or classes. See below for some notes about overloading classes, you strange person you.

The appropriate implementation is chosen based on the calling argument pattern.

For example:

>>> class A(object):
...   @overload
...   def method(self, a):
...     return 'a'
...   @method.add
...   def method(self, a, b):
...     return 'a, b'
...
>>> a = A()
>>> a.method(1)
'a'
>>> a.method(1, 2)
'a, b'

The overloading handles fixed, keyword, variable (*args) and arbitrary keyword (**keywords) arguments.

It also handles annotations if those annotations are types:

>>> @overload
... def func(a:int):
...   return 'int'
...
>>> @func.add
... def func(a:str):
...   return 'str'
...
>>> func(1)
'int'
>>> func('s')
'str'
>>> func(1.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "overload.py", line 94, in f
    raise TypeError('invalid call signature')
TypeError: invalid call signature

The docstring and name (ie. documentation) of the resultant callable will match that of the first callable overloaded.

Overloading Classes

Overloading classes allows you to select a class type based on the construction arguments of each alternative type’s __new__ method.

There’s a catch though: the __new__ method must explicitly invoke the base class __new__ method, rather than use super() like usual. This is because after being @overloaded the class is a function, and super() doesn’t like being passed functions. So instead of:

@overload
class A(object):
    def __new__(cls):
        # this will fail because "A" is a function now
        return super(A, cls).__new__(cls)

you must:

@overload
class A(object):
    def __new__(cls):
        # must explicitly reference the base class
        return object.__new__(cls)

I’ll leave it up to the reader to justify their use of @overloading classes.

Version History (in Brief)

  • 1.0 the initial release

See the end of the source file for the license of use.

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

overload-1.0.tar.gz (4.6 kB view details)

Uploaded Source

File details

Details for the file overload-1.0.tar.gz.

File metadata

  • Download URL: overload-1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for overload-1.0.tar.gz
Algorithm Hash digest
SHA256 ca07c7dfa79ac7ca85498e06b31dd340ba76e72297f287cfddb1001ab8d583fe
MD5 e18f475dbaf893fb1f94dbfa26c76519
BLAKE2b-256 0df03c9de1afe6f44c4ebbae8741536417d142cf657ebce24e7ff9f975f09353

See more details on using hashes here.

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