# 可迭代对象与迭代器

#### 可迭代对象

Example：

``````>>> from collections import Iterable
>>> isinstance([],Iterable)
True
>>> isinstance({}, Iterable)
True
>>> isinstance('test', Iterable)
True
>>> isinstance((x for x in range(10)), Iterable)
True
>>> isinstance(1, Iterable)
False

``````

#### 迭代器

Example:

``````>>> from collections import Iterator
>>> isinstance((x for x in range(10)), Iterator)
True
>>> isinstance([], Iterator)
False
>>> isinstance({}, Iterator)
False
>>> isinstance('abc', Iterator)
False
``````

#### 迭代器与列表

``````aList = ['a', 'b']
aIter = iter(aList)

while True:
try:
print aIter.next()
except StopIteration:
print 'Done'
break
``````

#### 迭代器与字典

``````aDict = {'a': 1, 'b': 2}

print 'key: %s, value: %s' % (key, value)
``````

#### for循环原理

``````for x in [1, 2, 3, 4, 5]:
pass
``````

``````# 首先获得Iterator对象:
it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
try:
# 获得下一个值:
x = next(it)
except StopIteration:
# 遇到StopIteration就退出循环
break
``````

#### 自定义迭代器

``````class MyRange(object):
def __init__(self, end):
self.curr = 0
self.end = end

def __iter__(self):
return self

def next(self):
if self.curr < self.end:
val = self.curr
self.curr += 1
return val
else:
raise StopIteration()

``````

## 生成器

Example:

``````def test():
for i in range(5):
yield i

a = test()
print a.next()
print a.next()
print a.next()
print a.next()
print a.next()
print a.next()
``````

``````0
1
2
3
4
Traceback (most recent call last):
File "test8.py", line 12, in <module>
print a.next()
StopIteration
``````

``````    def sorted_extensions(self):
if self.sorted_ext_list is None:
self.sorted_ext_list = sorted(self.extensions.iteritems())

for _alias, ext in self.sorted_ext_list:
yield ext

``````

get_resource方法中调用了此函数

``````    for ext in self.sorted_extensions():
resources.extend(ext.get_resources())
``````

#### 斐波拉契数列的实现

``````def fab(max):
n, a, b = 0, 0, 1
while n < max:
yield b
a, b = b, a+b
n += 1

``````

## 装饰器

``````def hello(fn):
def wrapper():
print "hello, %s" % fn.__name__
fn()
print "goodby, %s" % fn.__name__
return wrapper

@hello
def foo():
print "i am foo"

foo()
``````

``````➜  python python hello.py
hello, foo
i am foo
goodby, foo
``````

1）函数foo前面有个@hello的“注解”，hello就是我们前面定义的函数hello

2）在hello函数中，其需要一个fn的参数（这就用来做回调的函数）

3）hello函数中返回了一个inner函数wrapper，这个wrapper函数回调了传进来的fn，并在回调前后加了两条语句。

``````foo = hello(foo)
``````

``````# 理解函数其实是一个对象的概念
fn = foo
print "hello, %s" % fn.__name__
fn()
print "goodby, %s" % fn.__name__

``````

``````@decorator_one
@decorator_two
def func():
pass
``````

``````func = decorator_one(decorator_two(func))
``````

``````def makeHtmlTag(tag, *args, **kwds):
def real_decorator(fn):
css_class = " class='{0}'".format(kwds["css_class"]) \
if "css_class" in kwds else ""
def wrapped(*args, **kwds):
return "<"+tag+css_class+">" + fn(*args, **kwds) + "</"+tag+">"
return wrapped
return real_decorator

@makeHtmlTag(tag="b", css_class="bold_css")
@makeHtmlTag(tag="i", css_class="italic_css")
def hello():
return "hello world"

print hello()

# 输出：
# <b class='bold_css'><i class='italic_css'>hello world</i></b>
``````

``````class makeHtmlTagClass(object):

def __init__(self, tag, css_class=""):
self._tag = tag
self._css_class = " class='{0}'".format(css_class) \
if css_class !="" else ""

def __call__(self, fn):
def wrapped(*args, **kwargs):
return "<" + self._tag + self._css_class+">"  \
+ fn(*args, **kwargs) + "</" + self._tag + ">"
return wrapped

@makeHtmlTagClass(tag="b", css_class="bold_css")
@makeHtmlTagClass(tag="i", css_class="italic_css")
def hello(name):
return "Hello, {}".format(name)

print hello("Hao Chen")
``````

1）如果decorator有参数的话，init() 成员就不能传入fn了，而fn是在call的时候传入的。
2）这段代码还展示了 wrapped(*args, **kwargs) 这种方式来传递被decorator函数的参数。

#### functools的wraps

``````def hello(fn):
def wrapper():
print "hello, %s" % fn.__name__
fn()
print "goodby, %s" % fn.__name__
return wrapper

@hello
def foo():
print "i am foo"

foo()
print foo.__name__
``````

``````➜  python python hello.py
hello, foo
i am foo
goodby, foo
wrapper
``````

``````from functools import wraps
def hello(fn):
@wraps(fn)
def wrapper():
print "hello, %s" % fn.__name__
fn()
print "goodby, %s" % fn.__name__
return wrapper

@hello
def foo():
print "i am foo"

foo()
print foo.__name__
``````