FI-*_*nfo 14 python closures python-3.x
在以下示例中:
def speak(volume):
def whisper(text):
print(text.lower() + ('.' * volume))
def yell(text):
print (text.upper() + ('!' * volume))
if volume > 1:
return yell
elif volume <= 1:
return whisper
func = speak(volume=10)
func('hello')
HELLO!!!!!!!!!! # <== obviously `10` is stored in `func` somewhere
Run Code Online (Sandbox Code Playgroud)
给定func,我将如何获得“体积”?func命名空间中是否有东西可以赋予值10?我以为也许会在其中func.__globals__,func.__dict__但两者都不会。
bal*_*man 19
下面(下面的代码返回10)
func.__closure__[0].cell_contents
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
378 次 |
| 最近记录: |