Package: gf3
Overviewgflets you write generic functionsgeneric functionswith multi-methods, that dispatch on all their arguments.Simple Example>>> from gf import generic, method
>>> add = generic()
>>> type(add)
<class 'function'>Lets defineaddfor two integers:>>> @method()
... def add(n0: int, n1: int):
...     return n0 + n1Lets test it:>>> add(1, 2)
3Callingaddwith instances of other types fails:>>> add("Hello ", "World")
Traceback (most recent call last):
...
NotImplementedError: Generic '__main__.add' has no implementation for type(s): __builtin__.str, __builtin__.strOf courseaddcan also by defined for two strings:>>> @method()
... def add(s0: str, s1: str):
...     return s0 + s1Now our hello world example works:>>> add("Hello ", "World")
'Hello World'addcan also be defined for a string and an integer:>>> @method()
... def add(s: str, n: int):
...     return s + str(n)Thus we can add a string and a number:>>> add("You ", 2)
'You 2'Python’s Special Methodsgf.Objectimplements (nearly) all of thespecial instance
methods of a python objectas a generic function.
The package includes a rational number implementation that makes
heavy use of this feature:@method()def__add__(a:object,b:Rational):"""Add an object and a rational number.

    `a` is converted to a `Rational` and then both are added."""returnRational(a)+b@method(Rational,object)def__add__(a:Rational,b:object):"""Add a rational number and an object.

    `b` is converted to a `Rational` and then both are added."""returna+Rational(b)gf.Objectalso has a more Smalltalk means of overwritingobject.__str__andobject.__repr__using a file like object.
Again the rational example is instructive about its usage.@method()def__out__(rational:Rational,writer:Writer):"""Write a nice representation of the rational.

    Denominators that equal 1 are not printed."""writer("%d",rational.numerator)ifrational.denominator!=1:writer(" /%d",rational.denominator)@method()def__spy__(rational:Rational,writer:Writer):"""Write a debug representation of the rational."""writer("%s(",rational.__class__.__name__)ifrational.numerator!=0:writer("%r",rational.numerator)ifrational.denominator!=1:writer(",%r",rational.denominator)writer(")")InstallationAs usualgf3can be installed withpip, like this:pip install gf3DocumentationThe whole documentation is available at in the following formatsHTMLhttp://gf3.klix.ch(Also servers asgf’s homepage)PDFhttp://gf3.klix.ch/gf3.pdfChangesA short sketch of the changes done in each release.Release 0.2.4The following was changed in Release 0.2.4:Thepush-method accepts an identation string
for identing writers.The methodspushandpopnow accept
arbitrary arguments in the general case.Successfully tested the whole framework with Python 3.5.Release 0.2.3The following was changed in Release 0.2.3:Fixed the long description.Wrote some documentation about changing the implementation
class of a generic function.Release 0.2.2The following was changed in Release 0.2.2:Write more documentation.
Especially documented themergeand theisgenericfunctions.Consistency between the long text and on PyPi and the documentation.Release 0.2.1Needed to bump the version information, because the homepage
in the package-information was wrong[1]and a new upload was needed.Release 0.2.0The following was changed in Release 0.2.0:Ported the whole module to Python 3.6 and Python 3.7.Exclusively usesparameter annotationsto specify the types to dispatch on.Added standard conforming default implementations for methods
like__add__. All these methods now raise a properTypeErrorinstead of raising aNotImplementedError.Added some means to write generic functions that dispatch types only.
This is the generic function equivalent of a class-method.Added some means to dispatch on single objects.
This is the equivalent adding methods to class-instances[2].The package name for PyPi is nowgf3.Release 0.1.4The following was fixed in Release 0.1.4:Fixed an issue with variadic methods. Sometimes definitions
of variadic methods added after the method was already called
where not added.Specified and implemented a precedence rule for overlapping
variadic methods of generic functions.Improved generated documentation for variadic methods.Fixed the markup of some notes in the documentation.Release 0.1.3The following was changed in Release 0.1.3:Added variadic methods, e.g. multi-methods with a
variable number of arguments.Improved the long description text a bit
and fixed bug in its markup.Fixed invalid references in the long description.Release 0.1.2The following was changed in Release 0.1.2:Added a generic functions forgf.Object.__call__.Added agf.go.FinalizingMixin.gf.genericnow also accepts a type.Improved the exception information for ambiguous calls.Fixed some documentation glitches.Release 0.1.1This was the initial release.[1]Silly me discovered about the shutdown of pythonhosted.org
after version 0.2.0 was uploaded.[2]Of course this is not possible with standard python classes
and their instances.AcknowledgementsGuido van Rossum created the core of this package. I just renamed some things
and added some^H^H^H^H oodles of convenience stuff. Thank you Guido!Copyright© 2006-2013 Python Software Foundation.
© 2013-2018 Gerald Klix.