我有这个简单的功能:
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
的实际内容?