PEP 3124 是否有一些实施

发布于 2024-09-15 03:28:24 字数 170 浏览 6 评论 0原文

我正在搜索任何 PEP 3124 实施或开发过程。我对邮件列表不太熟悉,但去年Python邮件列表中似乎没有出现序列“3124”。是否有关于此 PEP 的一些信息?

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 is going on regarding this PEP ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-09-22 03:28:24

本 PEP 中描述的大部分功能已在开发中实现
PEAK-Rules 框架的版本。特别是基本的重载和方法
组合框架(减去 @overload 装饰器)已经存在。这
Peak.rules.core 中所有这些功能的实现需要 656 行 Python 代码
写作。

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

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

暮色兮凉城 2024-09-22 03:28:24

我刚刚读到有关 PEP 3124 的内容,心里想,“这听起来不太难实现”,并在阅读这个问题之前制定了一个解决方案。所以这就是(没有任何错误检查)。

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])

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])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文