【发布时间】:2021-06-29 03:42:50
【问题描述】:
如果我想用一个参数调用一个函数,而不使用任何花哨的东西,比如声明非局部变量或创建一个列表,我如何在每次调用我的函数时存储我的参数,以便它“记住”我之前的参数以前打过电话吗? 我的想法如下:
# stac(x) is a function that returns another function (nested) or itself.
>>> stac(4)(5)(6)(4)(6)(7)(8)(3)(0)
# if 0, prints the previous numbers that was called most recently to first call
3
8
7
.
.
4
我想到的类似的事情是检测 stac(x) 中是否有重复的参数,这样每次如果 x 匹配前面的参数之一,它就会打印出 x。
# stac(x) is a function that returns another function (nested) or itself.
>>> stac(1)(2)(3)(4)(1)(3)(5)(7)
1
3
# since 1 and 3 is repeated
我对如何处理这个想法的想法是,我们返回一个在当前帧中定义的函数,这样,如果我们在新帧中的参数满足条件(或不满足),它会在我们首先调用一个参数。虽然我不确定这是否有效。如果有人知道如何实现它,我将不胜感激!
【问题讨论】:
-
零清除堆栈吗?
-
我不确定这意味着什么,在我的脑海中,我只是认为如果我当前的参数为 0,那么它会返回以前的调用并沿途打印“以前的”参数
标签: python higher-order-functions