在Python中翻转函数的参数顺序

在Python中翻转函数的参数顺序,第1张

在Python中翻转函数参数顺序

您可以使用嵌套函数定义在Python中创建闭包。这使您可以创建一个新的函数来颠倒参数顺序,然后调用原始函数:

>>> from functools import wraps>>> def flip(func):        'Create a new function from the original with the arguments reversed'        @wraps(func)        def newfunc(*args): return func(*args[::-1])        return newfunc>>> def divide(a, b):        return a / b>>> new_divide = flip(divide)>>> new_divide(30.0, 10.0)0.3333333333333333


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5674884.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存