python 3中具有类似lisp的通用函数的包。

gf3的Python项目详细描述


概述

gf允许您编写泛型函数 generic functions 有了多个方法,就可以分派所有的参数。

简单示例

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

让我们为两个整数定义add

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

让我们测试一下:

>>> add(1, 2)
3

使用其他类型的实例调用add失败:

>>> add("Hello ", "World")
Traceback (most recent call last):
...
NotImplementedError: Generic '__main__.add' has no implementation for type(s): __builtin__.str, __builtin__.str

当然,add也可以定义为两个字符串:

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

现在我们的hello world示例可以工作了:

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

add也可以为字符串和整数定义:

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

因此,我们可以添加一个字符串和一个数字:

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

python的特殊方法

gf.object将(几乎)所有special instance methods of a python object作为泛型函数实现。 该包包括一个rational number实现,它使 大量使用此功能:

@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.object还有一种更简单的覆盖方法 对象。对象。使用类似文件的对象。 同样,rational示例对它的使用具有指导意义。

@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(")")

安装

像往常一样,gf3可以用pip安装,如下所示:

pip install gf3

文件

整个文档的格式如下

HTML
http://gf3.klix.ch (Also servers as gf’s homepage)
PDF
http://gf3.klix.ch/gf3.pdf

变化

每个版本中所做更改的简短草图。

释放0.2.4

在0.2.4版中更改了以下内容:

  • The push-method accepts an identation string for identing writers.
  • The methods push and pop now accept arbitrary arguments in the general case.
  • Successfully tested the whole framework with Python 3.5.

版本0.2.3

在0.2.3版中更改了以下内容:

  • Fixed the long description.
  • Wrote some documentation about changing the implementation class of a generic function.

版本0.2.2

在0.2.2版中更改了以下内容:

  • Write more documentation. Especially documented the merge and the isgeneric functions.
  • Consistency between the long text and on PyPi and the documentation.

版本0.2.1

需要碰撞版本信息,因为主页 包中的信息是错误的[1],需要新的上载。

释放0.2.0

在0.2.0版中更改了以下内容:

  • Ported the whole module to Python 3.6 and Python 3.7.
  • Exclusively uses parameter annotations to specify the types to dispatch on.
  • Added standard conforming default implementations for methods like __add__. All these methods now raise a proper TypeError instead of raising a NotImplementedError.
  • 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 now ^{tt1}$.

释放0.1.4

以下内容已在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.

版本0.1.3

以下内容在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.

版本0.1.2

在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.

发布0.1.1

这是最初的版本。

[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.

致谢

guido van rossum创建了这个包的核心。我刚把一些东西改名了 还加了很多方便的东西。谢谢你圭多!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java OpenGL着色器可在桌面上编译,但不能在Android上编译   java调度器。forward()生成HTTP 500   java从另一个类访问类   java EasyModBus Modbus客户端。ReadHoldingRegisters()返回意外的0   java 安卓bitmap低质量问题   带有signaturePropFile的java Axis2/rampart加密问题   Java,解析xml属性的原始未转换值。Jaxson或jaxb   爪哇太阳报。网inetaddr。ttl属性导致AccessControlException   java简单计算器无法通过println   线程安全java servlet   java从文本文件中获取特定信息   java打包具有字符串数组的哈希映射   java SQL合并以更新值或将值插入到同一个表中   java springrabitmq和线程本地何时是调用clean方法的好时机   windows java在我的64位计算机的任务管理器中显示为32位   java不确定使用什么值来初始化我的神经网络   java Maven插件下载文件