免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1480 | 回复: 7
打印 上一主题 下一主题

有一個小程序想問問大家的 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-25 00:26 |只看该作者 |倒序浏览
是書中的一個範例
我不太清楚這範例的目的要訓練甚麼的??
或者他想説明甚麼的??


輸出結果
ni
ni
84
84
100
120
g g g g g g g
a b c d e f g
a b c d e f g



def simple():
    spam = 'ni'
    def action():
        print(spam)          # name maps to enclosing function
    return action

act = simple()              # make and return nested function
act()                           # then call it: prints 'ni'




def normal():
    def action():
        return spam         # really, looked up when used
    spam = 'ni'
    return action

act = normal()
print(act())                # also prints 'ni'




def weird():
    spam = 42
    return (lambda: spam * 2)       # remembers spam in enclosing scope

act = weird()
print(act())    # prints 84




def weird():
    tmp = (lambda: spam * 2)        # remembers spam
    spam = 42                       # even though not set till here
    return tmp

act = weird()
print(act())                        # prints 84




def weird():
    spam = 42
    handler = (lambda: spam * 2)     # func doesn't save 42 now
    spam = 50
    print(handler())                 # prints 100: spam looked up now
    spam = 60
    print(handler())                 # prints 120: spam looked up again now

weird()




def odd():
    funcs = []
    for c in 'abcdefg':
       funcs.append((lambda: c))      # c will be looked up later
    return funcs                      # does not remember current c

for func in odd():
    print(func(), end=' ')            # print 7 g's, not a,b,c,... !




print()
def odd():
    funcs = []
    for c in 'abcdefg':
       funcs.append((lambda c=c: c))    # force to remember c now
    return funcs                        # defaults eval now

for func in odd():
    print(func(), end=' ')              # OK: now prints a,b,c,...




print()
funcs = []                              # enclosing scope is module
for c in 'abcdefg':                     # force to remember c now
   funcs.append((lambda c=c: c))        # else prints 7 g's again

for func in funcs:
    print(func(), end=' ')              # OK: prints a,b,c,...

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2012-03-25 11:05 |只看该作者
没有任何说明就冒出一堆范例?这书的名字叫什么,要声讨!

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
3 [报告]
发表于 2012-03-25 14:31 |只看该作者
铜球书名

论坛徽章:
0
4 [报告]
发表于 2012-03-25 18:49 |只看该作者
首先要弄明白的是,搂住在看到什么内容的时候看到的这个范例吧?
然后再根据他看到的内容,来说吧?

论坛徽章:
0
5 [报告]
发表于 2012-03-25 23:27 |只看该作者
Oreilly出版社 書名Programming Python
這是gui章節中的  説明這program的標題是這樣寫的: But you must still sometimes use defults instead of enclosing scope

论坛徽章:
0
6 [报告]
发表于 2012-03-26 11:44 |只看该作者
回复 5# jack-1991


就这样一句? 前面应该还有吧。这个应该是讲和闭包(Closure)有关的问题。

论坛徽章:
0
7 [报告]
发表于 2012-03-26 23:26 |只看该作者
同一個programming 用幾個方法去達成從而説明分別...
大約是這様的

1: argument versus globals

2 passing in enclosing scope values with default arguments

3: passing in enclosing scope values with automatic references

4: But you must still sometimes use defults instead of enclosing scope

论坛徽章:
0
8 [报告]
发表于 2012-03-26 23:29 |只看该作者
不過真的謝謝你這麼用心的幫助!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP