Some docstrings updated.

This commit is contained in:
Andrey Popp 2010-07-19 13:20:20 +04:00
parent bd7f81f852
commit 93f9a70260
2 changed files with 32 additions and 10 deletions

@ -12,16 +12,27 @@ __all__ = ["Dispatcher", "multifunction"]
def multifunction(*argtypes):
""" Declare function as multifunction."""
""" Declare function as multifunction.
This decorator takes ``*argtypes`` argument types and replace decorated
function with :class:`generic.multidispatch.FunctionDispatcher` object,
which is responsible for multiple dispatch feature.
"""
def _replace_with_dispatcher(func):
dispatcher = _make_dispatcher(Dispatcher, func, len(argtypes))
dispatcher = _make_dispatcher(FunctionDispatcher, func, len(argtypes))
dispatcher.register_rule(func, *argtypes)
return dispatcher
return _replace_with_dispatcher
def multimethod(*argtypes):
""" Declare method as multimethod."""
""" Declare method as multimethod.
This decorator works exactly the same as
:func:`generic.multidispatch.multifunction` decorator but replaces
decorated method with :class:`generic.multidispatch.MethodDispatcher`
object instead.
"""
def _replace_with_dispatcher(func):
dispatcher = _make_dispatcher(MethodDispatcher, func, len(argtypes) + 1)
dispatcher.register_unbound_rule(func, *argtypes)
@ -37,8 +48,12 @@ def has_multimethods(cls):
return cls
class Dispatcher(object):
""" Function call dispatcher based on argument types."""
class FunctionDispatcher(object):
""" Multiple dispatch for functions.
This object dispatch calls to function by its argument types. Usually it is
produced by :func:`generic.multidispatch.multifunction` decorator.
"""
def __init__(self, argspec, params_arity):
""" Initialize dispatcher with ``argspec`` of type
@ -103,10 +118,16 @@ class Dispatcher(object):
return rule(*args, **kwargs)
class MethodDispatcher(Dispatcher):
class MethodDispatcher(FunctionDispatcher):
""" Multiple dispatch for methods.
This object dispatch call to method by its class and arguments types.
Usually it is produced by :func:`generic.multidispatch.multimethod`
decorator.
"""
def __init__(self, argspec, params_arity):
Dispatcher.__init__(self, argspec, params_arity)
FunctionDispatcher.__init__(self, argspec, params_arity)
# some data, that should be local to thread of execution
self.local = threading.local()

@ -10,9 +10,10 @@ class DispatcherTests(unittest.TestCase):
def createDispatcher(self, params_arity, args=None, varargs=None,
keywords=None, defaults=None):
from inspect import ArgSpec
from generic.multidispatch import Dispatcher
return Dispatcher(ArgSpec(args=args, varargs=varargs, keywords=keywords,
defaults=defaults), params_arity)
from generic.multidispatch import FunctionDispatcher
return FunctionDispatcher(ArgSpec(args=args, varargs=varargs,
keywords=keywords,
defaults=defaults), params_arity)
def test_one_argument(self):