Skip to main content

Overview

gf lets you write generic functions generic functions with multi-methods, that dispatch on all their arguments.

Simple Example

>>> from gf import generic, method
>>> add = generic()
>>> type(add)
<type 'function'>

Lets define add for two integers:

>>> @method(int, int)
... def add(n0, n1):
...     return n0 + n1

Lets test it:

>>> add(1, 2)
3

Calling add with 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__.str

Of course add can also by defined for two strings:

>>> @method(basestring, basestring)
... def add(s0, s1):
...     return s0 + s1

Now our hello world example works:

>>> add("Hello ", "World")
'Hello World'

add can also be defined for a string and an integer:

>>> @method(basestring, int)
... def add(s, n):
...     return s + str(n)

Thus we can add a string and a number:

>>> add("You ", 2)
'You 2'

Python’s Special Methods

gf.Object implements (nearly) all of the special instance methods of a python object as a generic function. The package includes a rational number implementation that makes heavy use of this feature:

@method(object, Rational)
def __add__(a, b):
    """Add an object and a rational number.

    `a` is converted to a `Rational` and then both are added."""
    return Rational(a) + b

@method(Rational, object)
def __add__(a, b):
    """Add a rational number and an object.

    `b` is converted to a `Rational` and then both are added."""
    return a + Rational(b)

gf.Object also has a more Smalltalk means of overwriting object.__str__ and object.__repr__ using a file like object. Again the rational example is instructive about its usage.

@method(Rational, Writer)
def __out__(rational, writer):
    """Write a nice representation of the rational.

    Denominators that equal 1 are not printed."""
    writer("%d", rational.numerator)
    if rational.denominator != 1:
        writer(" / %d", rational.denominator)

@method(Rational, Writer)
def __spy__(rational, writer):
    """Write a debug representation of the rational."""
    writer("%s(", rational.__class__.__name__)
    if rational.numerator != 0:
            writer("%r", rational.numerator)
            if rational.denominator != 1:
                writer(", %r", rational.denominator)
    writer(")")

Changes

A short sketch of the changes done in each release.

Release 0.1.3

The 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.2

The following was changed in Release 0.1.2:

  • Added a generic functions for gf.Object.__call__.

  • Added a gf.go.FinalizingMixin.

  • gf.generic now also accepts a type.

  • Improved the exception information for ambiguous calls.

  • Fixed some documentation glitches.

Release 0.1.1

This was the initial release.

Acknowledgements

Guido van Rossum created the core of this package. I just renamed some things and added some convenience stuff. Thank you Guido!

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gf-0.1.3.tar.gz (21.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gf-0.1.3-py2.7.egg (33.4 kB view details)

Uploaded Egg

File details

Details for the file gf-0.1.3.tar.gz.

File metadata

  • Download URL: gf-0.1.3.tar.gz
  • Upload date:
  • Size: 21.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for gf-0.1.3.tar.gz
Algorithm Hash digest
SHA256 5980d2dac7ca04c3c5e1c91342adcba5872f0ce542f999c1e6c779ec95686e4f
MD5 595a2ed86a9d3b85d2c4b6153ebb23fb
BLAKE2b-256 7303d6c625d118c2774d73bb37b798f57cdf474b02d9d7330795e0c075485953

See more details on using hashes here.

File details

Details for the file gf-0.1.3-py2.7.egg.

File metadata

  • Download URL: gf-0.1.3-py2.7.egg
  • Upload date:
  • Size: 33.4 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for gf-0.1.3-py2.7.egg
Algorithm Hash digest
SHA256 2f2917488062f7abcdc60ffd51c7f8a4f5416c90adf3f5622994a51cdcc0ef4b
MD5 8079dc1481055ac7a69f16587538012e
BLAKE2b-256 21f961f82a3228df55600eddad5e0ae82566544464566b62dba69415880e4136

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page