开发者

Is there some implementation of PEP 3124

I'm searching for any PEP 3124 implemenation or development process. I'm not very familliar with mailing list, but it seems that sequence "3124" have not appeared in Python mailing list during last year. Is there some information what i开发者_JAVA技巧s going on regarding this PEP ?


Most of the functionality described in this PEP is already implemented in the in-development version of the PEAK-Rules framework. In particular, the basic overloading and method combination framework (minus the @overload decorator) already exists there. The implementation of all of these features in peak.rules.core is 656 lines of Python at this writing.

http://www.python.org/dev/peps/pep-3124/#implementation-notes


I was just reading about PEP 3124 and thought to myself, "That doesn't sound too hard to implement" and worked out a solution before reading this SO question. So here it is (without any error checking).

def overload(f):
    oldfunc = globals()[f.__name__]
    param1 = f.__code__.co_varnames[0]
    type1 = f.__annotations__[param1]
    def impl(*args, **kwargs):
        if param1 in kwargs:
            arg1 = kwargs[param1]
        else:
            arg1 = args[0]
        if isinstance(arg1, type1):
            return f(*args, **kwargs)
        else:
            return oldfunc(*args, **kwargs)
    impl.__name__ = f.__name__
    return impl

from math import sin
import numpy

@overload
def sin(x: numpy.ndarray):
    return numpy.sin(x)

sin(3.14)
# 0.0015926529164868282

sin(numpy.arange(10))
# array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
#        -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜