[英]calling enclosing functions in python
在使用封閉函數的方式中,當我使用調用方法時,它不起作用。
def foo():
x = 20
def bar():
return x
bar()
在這里,當我調用foo()時,它返回None。 當我在foo中使用“return bar”而不是“bar()”時,它可以很好地調用foo()(),那么為什么return不能用於封閉函數但是打印工作?
foo()
不返回bar()
的值,因為你沒有將它作為foo()
的返回值傳遞。
def foo():
x = 20
def bar():
return x
bar()
# No return value for foo, this equals return None
return None
res = foo()
type(res)
# NoneType
如果你想通過foo()
傳播bar()
的返回值,你需要使用return
:
def foo():
x = 20
def bar():
return x
return bar()
res = foo()
x
# 20
bar()
是一個表達式, return bar()
是一個語句。 除了在lambda中,我們需要return語句來返回一些東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.