def maker(n):
def action(x):
return x ** n
return action
f = maker(2)
print(f)
print(f(3))
print(f(4))
g = maker(3)
print(g(3))
print(f(3)) # still remembers 2
Run Code Online (Sandbox Code Playgroud)
为什么嵌套函数会记住第一个值,2
即使maker()
已经返回并退出时action()
被调用?
道歉这是一个非常广泛的问题.
下面的代码是网络上发现的一些内容.我感兴趣的关键是从@protected开始的行 - 我想知道这是做什么以及它是如何做到的?它似乎是在执行do_upload_ajax函数之前检查有效用户是否已登录.这看起来像是一种非常有效的用户身份验证方式.我不明白这个@函数的机制 - 有人能引导我朝着正确的方向解释如何在现实世界中实现它吗?Python 3请回答.谢谢.
@bottle.route('/ajaxupload', method='POST')
@protected(check_valid_user)
def do_upload_ajax():
data = bottle.request.files.get('data')
if data.file:
size = 0
Run Code Online (Sandbox Code Playgroud) 考虑 pygame 循环中的这一行:
pygame.display.set_mode().fill((0, 200, 255))
Run Code Online (Sandbox Code Playgroud)
来自:http : //openbookproject.net/thinkcs/python/english3e/pygame.html
一、你怎么知道set_mode中嵌套了一个填充函数?我在 pygame 文档中搜索过,在 set_mode 部分中没有关于填充的信息。
二、set_mode()
是pygame包的显示模块的一个函数。如何调用嵌套在另一个函数中的函数?我怎么能用print"hi"
这个函数调用(尝试过但得到一个 AttributeError):
def f():
def g():
print "hi"
Run Code Online (Sandbox Code Playgroud)