1. 程式人生 > >讀書筆記--《Python基礎教程第二版》--第六章 抽象

讀書筆記--《Python基礎教程第二版》--第六章 抽象

ja

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



本文出自 “小魚的博客” 博客,謝絕轉載!

讀書筆記--《Python基礎教程第二版》--第六章 抽象