包含函数的列表返回为None

时间:2016-09-06 11:04:20

标签: python-3.x return nonetype

我有这个简单的功能:

def double(x)   : return x*2
def triple(x)   : return x*3
def increment(x): return x+1

def find(n, moves=[]):
    if n == 1:
        print("Inside if statement with return")
        print("`moves` is not None: moves=", moves)
        return moves
    if n % 2 == 0:
        find(n // 2, moves + [double])
    elif n % 3 == 0:
        find(n // 3, moves + [triple])
    else:
        find(n - 1 , moves + [increment])


print("find has returned: ", find(23))

我希望函数列表作为返回值,但实际输出是:

Inside if statement with return
`moves` is not None: moves= [<function increment at 0x7f03e3944598>, <function double at 0x7f03e9f6cbf8>, <function increment at 0x7f03e3944598>, <function double at 0x7f03e9f6cbf8>, <function increment at 0x7f03e3944598>, <function double at 0x7f03e9f6cbf8>, <function double at 0x7f03e9f6cbf8>]
find has returned:  None

这是非常意外的,因为moves在最终None语句中不是if,如print语句所示,但是返回None。

[:]未更改任何内容后添加return moves,仍会返回None

我如何return moves的实际内容?

0 个答案:

没有答案