小编gym*_*y42的帖子

递归传递lambda

我正在尝试传递lambda。

def summation(n, term):
    if (n == 0):
        return
    else:
        return summation(n - 1, term) + term

print(summation(5, lambda x: x + 1))            
Run Code Online (Sandbox Code Playgroud)

据我了解,lambda x : x + 1将成为termterm将评估到n + 1的功能,但是这是行不通的。谁能向我解释?

python

1
推荐指数
1
解决办法
32
查看次数

How does this array variable reference act?

I was wondering how something like this would work.

int[] a = {5, 3, 4};

int[] b = {3, 4, 5};

a = b;
Run Code Online (Sandbox Code Playgroud)

Does this mean that a will now reference b. So if I do a[0] it will be 3?

Also if this is the case what happens to the items in the old array?

java

0
推荐指数
1
解决办法
58
查看次数

一行上的Python变量分配

为什么这样做:

def make_fib():
    cur, next = 0, 1
    def fib():
        nonlocal cur, next
        result = cur
        cur, next = next, cur + next
        return result
    return fib
Run Code Online (Sandbox Code Playgroud)

工作方式不同于:

def make_fib():
    cur, next = 0, 1
    def fib():
        nonlocal cur, next
        result = cur
        cur = next
        next = cur + next
        return result
    return fib
Run Code Online (Sandbox Code Playgroud)

我看到第二个怎么弄乱了,因为在cur = next和next = cur + next时,因为本质上它将变成next = next + next,但是第一个为什么运行不同?

python

0
推荐指数
1
解决办法
23
查看次数

标签 统计

python ×2

java ×1