Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.6 / 3....
  • Linux Kernel 3.0.82 LTS...
  • KDE Software Compilatio...
  • PulseAudio 4.0
  • Wireshark 1.10.0
  • NetworkManager 0.9.8.2
  • LibreOffice 3.6.6 / 4.0...
  • SystemRescueCd 3.7.0
  • Linux Kernel 3.10 RC6
  • Ubuntu Tweak 0.8.5
  • Home > Linux > Programming > Libraries

    overload 1.1

    Download button

    No screenshots available
    Downloads: 122  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Richard Jones | More programs
    BSD License / FREE
    May 5th, 2011, 12:57 GMT [view history]
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    overload description

    Simple overloading of methods and functions through an @overload decorator

    overload is a simple overloading of methods and 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 argument(s)')
    TypeError: invalid call argument(s)


    This feature (and currently the module in general) requires Python 3.

    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.


    Product's homepage

    Requirements:

    · Python

    What's New in This Release: [ read full changelog ]

    · This version altered the text of the invalid call TypeError. Removed debug prints.

      


    TAGS:

    overload methods | overload functions | methods | functions | overload

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM