根据 Go by Example 里的 Closures 实现,想用 Python 实现一下, 没有成功
package main
import "fmt"
func intSeq() func() int {
i := 0
return func() int {
i += 1
return i
}
}
func main() {
nextInt := intSeq()
fmt.Println(nextInt())
fmt.Println(nextInt())
fmt.Println(nextInt())
fmt.Println(nextInt())
// 一脸懵 为什么不重新初始化 intSeq(),值会一直添加???
newInts := intSeq()
fmt.Println(newInts())
}
输出
1
2
3
4
1
# coding: utf-8
def intSeq():
def func():
i = 0
i += 1
return i
return func
if __name__ == "__main__":
nextInt = intSeq()
print nextInt()
print nextInt()
print nextInt()
print nextInt()
newInts = intSeq()
print nextInt()
输出结果:
1
1
1
1
1
话说 Python 中要怎么实现?
1
Arnie97 2017-12-22 00:22:38 +08:00 via Android 1
def seq():
....i = 0 ....def func(): ........i += 1 ........return i ....return func |
2
rabbbit 2017-12-22 00:27:18 +08:00 1
def intSeq():
i = [0] def func(): i[0] += 1 return i[0] return func if __name__ == "__main__": nextInt = intSeq() print nextInt() print nextInt() print nextInt() print nextInt() newInts = intSeq() print nextInt() |
3
awker OP |
4
Arnie97 2017-12-22 00:32:14 +08:00 via Android 1
一楼笔误,少贴了一行 nonlocal。
def seq(): ....i = 0 ....def func(): ........nonlocal i ........i += 1 ........return i ....return func |
5
rabbbit 2017-12-22 00:33:12 +08:00 2
|
6
rabbbit 2017-12-22 00:36:29 +08:00 1
newInts = intSeq()
print nextInt() to nextInt = intSeq() print nextInt() |
8
awker OP |
9
xpresslink 2017-12-22 12:44:52 +08:00 1
因为 Python 不需这么玩唉
Python 的哲学是不要自己造轮子。 >>> def g_factory(): i = 1 while True: yield i i += 1 >>> g = g_factory() >>> next(g) 1 >>> next(g) 2 >>> >>> from itertools import count >>> c = count(1) >>> next(c) 1 >>> next(c) 2 >>> >>> from itertools import cycle >>> c = cycle([1,2,3]) >>> next(c) 1 >>> next(c) 2 >>> next(c) 3 >>> next(c) 1 >>> |
10
julyclyde 2017-12-22 13:01:15 +08:00 2
为什么大家这么爱好实现一个有状态的函数?
这需求不是应该用对象吗? |
11
araraloren 2017-12-22 13:50:01 +08:00 1
@xpresslink The key point is not `how to implement a generator`.
|
12
araraloren 2017-12-22 13:51:55 +08:00 1
|
13
xpresslink 2017-12-22 14:33:57 +08:00 1
@araraloren To implement a generator is must-know trick for pythoneer, the key point is 'how to solve the problem or fulfill the requirement. you are using Python, you should obey its philosophy. generator is better solution, if you just want better enclosure go lisp.
|
14
bonfy 2017-12-22 17:09:52 +08:00
yield 吧... python generator
|
15
quinoa42 2017-12-22 21:00:50 +08:00 1
@julyclyde object 和 closure 分别是 OOP 和 FP 对同一需求的不同实现,只不过现在很多语言同时支持这两种罢了
first class function (或者,按 python 的说法,functions as first class objects )都是需要语言支持 closure 才能实现的 |
16
julyclyde 2017-12-24 10:43:42 +08:00
@quinoa42 可是它表现为一个 function,而 function “应该是”无状态的,它的所有输入都应该被明确列出
|