`
feisuzhu
  • 浏览: 13851 次
  • 性别: Icon_minigender_1
  • 来自: 济南
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

感悟Python的closure

阅读更多
最近在用django做一个小东西,目录结构什么的都设置好了,要写views.py。
因为仅仅是测试,所以输出一个字符串表示一下就可以了,于是开始:

def index(request):
    return HttpResponse("index page!")

def foo(request):
    return HttpResponse("foo page!")

def bar(request):
    return HttpResponse("bar page!")

......


我可没兴趣写这么蛋疼的东西,于是希望通过一个列表自动生成这些函数。
折腾了一会,得出了以下的代码:


# Create your views here.
from django.http import HttpResponse
import sys

def gen(lst):
    for i in lst:
        def _f(request):
            return HttpResponse("%s page!" % i)
        _f.func_name = i
        yield _f

l = [
    'index',
    'purchase',
    'message',
    'setting',
    'message_post',
    'message_anonpost',
    'setting_update',
    'ajax_history',
    'ajax_purchase',
    'ajax_message_setviewed'
]

for i in gen(l):
    sys.modules[__name__].__dict__[i.func_name] = i


恩, 看起来很让人满意,于是测试一下,发现所有的view都只显示"ajax_message_setviewed page!",也就是最后一个。
搞的我挺郁闷,这是怎么回事呢。。。。。。

仔细做实验外加分析了一下,发现所有的函数共享这同一个i变量(就是gen函数里的i)。

恩 因为gen函数只调用了一次,所以i变量就只创建了一次,所以所有的函数都引用同一个i了。

知道症结后,改写代码如下:

# Create your views here.
from django.http import HttpResponse
import sys

def gen(fn):
    def _f(request):
        return HttpResponse("%s page!" % fn)
    _f.func_name = fn
    return _f

l = [
    'index',
    'purchase',
    'message',
    'setting',
    'message_post',
    'message_anonpost',
    'setting_update',
    'ajax_history',
    'ajax_purchase',
    'ajax_message_setviewed'
]

for i in l:
    sys.modules[__name__].__dict__[i] = gen(i)

问题解决了
0
0
分享到:
评论

相关推荐

    Python closure闭包解释及其注意点详解

    主要介绍了Python closure闭包解释及其注意点详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Python闭包实例closure.py

    Python闭包实例closure.py 简单示例闭包的使用 简单示例闭包的使用

    python学习-07-closure-deco.zip

    Python是一种解释型的、面向对象的、带有动态语义的高级程序设计语言。它是由荷兰人吉多·罗萨姆于1989年发布的,第一个公开发行版发行于1991年。Python注重解决问题的方法,而不是语法和结构。它被广泛应用于各个...

    Python库 | closure-20180204-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:closure-20180204-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    closure闭包

    求文法的closure闭包,针对每个产生式,求其closure闭包,并打印输出

    Closure The Definitive Guide

    If you're ready to use Closure to build rich web applications with JavaScript, this hands-on guide has precisely what you need to learn this suite of tools in depth. Closure makes it easy for ...

    Closure Linter完整安装包

    Closure Linter完整安装包,包含安装说明。 python-2.7.3.msi setuptools-0.6c11.win32-py2.7.exe

    Python岗位常规面试题.pdf

    4. 什么是Python中的闭包(Closure)?请举例解释在Python中如何使用闭包。 5. 请解释Python中的多线程和多进程的区别,以及如何在Python中实现多线程和多进程。 6. Python中的什么机制实现了面向对象编程(Object-...

    Closure tools

    Closure toolsClosure toolsClosure toolsClosure tools

    python中闭包Closure函数作为返回值的方法示例

    闭包(closure)是函数式编程的重要的语法结构,Python也支持这一特性,下面这篇文章主要给大家介绍了关于python中闭包Closure函数作为返回值的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。

    Closure-Table-ClosureTable.rar

    主要完成了数据库存储概念中Closure-Table 存储,写了一个SSM简单的项目 项目是github上的,github也是我自己的项目

    Google Javascript Closure Compiler

    closure-compiler-v20170521.jar,以及一个.chm使用说明:‘Getting Started with the Closure Compiler Application’,‘Advanced Compilation and Externs’,‘Understanding the Restrictions Imposed by the ...

    python中函数常见坑

    博客配套文件,详细演示了局部变量作用域问题和迭代器反复调用问题,并给出了对应解决办法,供参考。

    Closure Compiler exe

    Closure Compiler exe 根据[在项目中使用Google Closure Compiler](http://www.cnblogs.com/JeffreyZhao/archive/2009/12/09/ikvm-google-closure-compiler.html)

    closure dependency not found解决包

    当我们从github上下载了blockly之后,打卡demos下的index.html时,选择blockly-developer-tools时会弹出一个对话框(大体内容是closure dependency not found),此时我们需要下载这个文件,解压并且命名为closure-...

    Javascript Closure

    Closure: the definitive guide, by Michael Bolin

    Closure: The Definitive Guide

    Closure_ The Definitive Guide js的闭包

Global site tag (gtag.js) - Google Analytics