6.1 懒惰即美德
>>> fibs=[0,1]
>>> for i in range(8):
... fibs.append(fibs[-2]+fibs[-1])
...
>>> fibs
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
6.2 抽象和结构
page=download_page()
freqs=compute_frequencies(page)
for word,freq in freqs:
print word,freq
6.3 创建函数
>>> import math
>>> x=1
>>> y=math.sqrt
>>> callable(x)
False
>>> callable(y)
True
>>> def hello(name):
... return ‘hello,‘+name +‘!‘
...
>>> print hello(‘world‘)
hello,world!
>>> print hello(‘Gumby‘)
hello,Gumby!
>>> def fibs(num):
... result=[0,1]
... for i in range(num-2):
... result.append(result[-2]+result[-1])
... return result
...
>>> fibs(10)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> fibs(15)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
6.3.1 记录函数
>>> def square(x):
... ‘Calcutes the square of the number x.‘
... return x*x
...
>>> square.__doc__
‘Calcutes the square of the number x.‘
>>> help(square)
Help on function square in module __main__:
square(x)
Calcutes the square of the number x.
6.3.2 并非真正的函数的函数
>>> def test():
... print ‘This is printed‘
... return
... print ‘This is not‘
...
>>> x = test()
This is printed
>>> print x
None
6.4 参数魔法
6.4.1 值从哪里来?
6.4.2 我能改变参数吗?
>>> def try_to_change(n):
... n=‘Mr. Gumby‘
...
>>> name=‘Mrs. Entity‘
>>> try_to_change(name) #n=name,改变n,name不变
>>> name
‘Mrs. Entity‘
字符串(以及数字和元组)是不可改变的
>>>
>>> def change(n):
... n[0]=‘Mr.Gumby‘
...
>>> names=[‘Mrs. Entiry‘,‘Mrs. Thing‘]
>>> change(names) #n=name,改变n,names变
>>> names
[‘Mr.Gumby‘, ‘Mrs. Thing‘]
解决方法:
>>> names=[‘Mrs. Entiry‘,‘Mrs. Thing‘]
>>> change(names[:])
>>> names
[‘Mrs. Entiry‘, ‘Mrs. Thing‘]
>>> change(names)
>>> names
[‘Mr.Gumby‘, ‘Mrs. Thing‘]
>>> names=[‘Mrs. Entiry‘,‘Mrs. Thing‘]
>>> n=names[:]
>>> n is names
False
>>> n == names
True
1、为什么我要修改参数
>>> storage={}
>>> storage[‘first‘]={}
>>> storage[‘middle‘]={}
>>> storage[‘last‘]={}
>>> me=‘Magnus Lie hetland‘
>>> storage[‘first‘][‘Magus‘]=[me]
>>> storage[‘middle‘][‘Lie‘]=[me]
>>> storage[‘last‘][‘hetland‘]=[me]
>>> storage
{‘middle‘: {‘Lie‘: [‘Magnus Lie hetland‘]}, ‘last‘: {‘hetland‘: [‘Magnus Lie hetland‘]}, ‘first‘: {‘Magus‘: [‘Magnus Lie hetland‘]}}
>>> my_sister=‘Anne Lie hetland‘
>>> storage[‘first‘].setdefault(‘Anne‘,[]).append(my_sister)
>>> storage[‘middle‘].setdefault(‘Lie‘,[]).append(my_sister)
>>> storage[‘last‘].setdefault(‘hetland‘,[]).append(my_sister)
>>> storage
{‘middle‘: {‘Lie‘: [‘Magnus Lie hetland‘, ‘Anne Lie hetland‘]}, ‘last‘: {‘hetland‘: [‘Magnus Lie hetland‘, ‘Anne Lie hetland‘]}, ‘first‘: {‘Magus‘: [‘Magnus Lie hetland‘], ‘Anne‘: [‘Anne Lie hetland‘]}}
>>> storage[‘middle‘][‘Lie‘]
[‘Magnus Lie hetland‘, ‘Anne Lie hetland‘]
>>> storage[‘first‘][‘Anne‘]
[‘Anne Lie hetland‘]
__author__ = ‘Administrator‘
def init(data):
data[‘first‘]={}
data[‘middle‘]={}
data[‘last‘]={}
def lookup(data,label,name):
return data[label].get(name)
def store(data,full_name):
names=full_name.split()
if 2 == len(names):names.insert(1,‘‘)
labels=‘first‘,‘middle‘,‘last‘
for label,name in zip(labels,names):
people=lookup(data,label,name)
if people:
people.append(full_name)
else:
data[label][name]=[full_name]
if __name__ == ‘__main__‘:
mynames={}
init(mynames)
store(mynames,‘Magnus Lie Hetland‘)
lookup(mynames,‘middle‘,‘Lie‘)
store(mynames,‘Robin Hood‘)
store(mynames,‘Robin Locksley‘)
store(mynames,‘Mr.Gumby‘)
lookup(mynames,‘first‘,‘Robin‘) #[‘Robin Hood‘, ‘Robin Locksley‘]
2、如果我的参数不可变呢
改变参数,必须用返回值或者列表、字典
>>> def inc(x):return x +1
...
>>> foo=10
>>> foo=inc(foo)
>>> foo
11
>>> def inc(x):x[0]=x[0]+1
...
>>> foo=[10]
>>> inc(foo)
>>> foo