码迷,mamicode.com
首页 > 编程语言 > 详细

读书笔记--《Python基础教程第二版》--第六章 抽象

时间:2017-09-09 09:44:35      阅读:243      评论:0      收藏:0      [点我收藏+]

标签: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基础教程第二版》--第六章 抽象

标签:ja

原文地址:http://395469372.blog.51cto.com/1150982/1963856

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!