notes on python

iterator

Behind the scenes, the for statement calls iter() on the container object. The function returns an iterator object that defines the method __next__() which accesses elements in the container one at a time. When there are no more elements, __next__() raises a StopIteration exception which tells the for loop to terminate.You can call the __next__() method using the next() built-in function

可以自己实现一个类的for loop:

class A:
	class it:
		def __init__(s,a):
			s.a=a
			s.l=len(s.a.l)+len(s.a.l2)
			s.i=0
		def __next__(s):
			if s.i==s.l:
				raise StopIteration
			a=0
			if s.i<len(s.a.l):
				a= s.a.l[s.i]
			else:
				a= s.a.l2[s.i-len(s.a.l)]
			s.i=s.i+1
			return a
	def __init__(s):
		s.l=[1,2,4,5]
		s.l2=[4,3,2,0]
	def __iter__(s):
		return A.it(s)

>>> a=A()
>>> [i for i in a]
[1, 2, 4, 5, 4, 3, 2, 0]
generator

Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the yield statement whenever they want to return data. Each time next() is called on it, the generator resumes where it left off (it remembers all the data values and which statement was last executed).

def gen(a):
	for i in a.l:
		yield i
	for i in a.l2:
		yield i

>>>for i in gen(a):
	print (i,end=',')

1,2,4,5,4,3,2,0,
generator expression
def gen():
	return ((i,j)for i in range(10)for j in range(9))
或:
for a in((i,j)for i in range(10)for j in range(9)):
	print (a)

for a in (...generator exp...)是在每次call next()时才生成a,而for a in [...list comprehension...]是提前就生成一个列表,据说前者效率更高

class and instance variables

我的理解,对于mutable变量,对象复制类的变量的一个“指针“:

>>> class A:
	li=[]
	def add(s,x):
		s.li.append(x)

		
>>> a=A()
>>> a.add(3)
>>> a.li
[3]
>>> b=A()
>>> b.add(4)
>>> b.li#b.li像是A.li的一个引用,已经被a.li修改过
[3, 4]
正确的做法:
>>> class A:
	def __init__(s):
		s.li=[]#每次创建对象都生成一个对象自己的列表
	def add(s,x):
		s.li.append(x)

		
>>> a=A()
>>> a.add(3)
>>> a.li
[3]
>>> b=A()
>>> b.add(4)
>>> b.li
[4]

而对于immutable 变量则类似于按值传递(尽管python中没有这些概念),不会有这个问题

函数嵌套
>>> def fun_gen(c):
...     def fun(x):
...             return x+c
...     return fun
... 
>>> f=fun_gen(4)
>>> f(5)
9
>>> fun_gen(5)(8)
13




转载于:https://my.oschina.net/soitravel/blog/358335

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值