Chinaunix

标题: 这个例子没看明白,谁解释一下 [打印本页]

作者: ymr    时间: 2009-05-30 02:39
提示: 作者被禁止或删除 内容自动屏蔽
作者: pastebt    时间: 2009-05-30 02:49
标题: 那里不明白呢?
那里不明白呢?
作者: ymr    时间: 2009-05-30 03:03
提示: 作者被禁止或删除 内容自动屏蔽
作者: pastebt    时间: 2009-05-30 04:39
标题: ietr is a build-in function
iter(o[, sentinel])

    Return an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel, is given, then o must be a callable object. The iterator created in this case will call o with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

也就是说
i = iter(x)
之后,因为x有__getitem__, i就是iterable的了

[ 本帖最后由 pastebt 于 2009-5-30 04:43 编辑 ]
作者: ymr    时间: 2009-05-30 05:06
提示: 作者被禁止或删除 内容自动屏蔽
作者: izhier    时间: 2009-05-30 09:08
四楼中:
or it must support the sequence protocol
(the __getitem__() method with integer arguments starting at 0).




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2