当前位置:编程学习 > python >>

Beginning Python - Chapter6 : Abstraction

#Chapter5:abstraction
#1 Creating Your Own Functions
# -1.1- Documenting Functions
def hello(name):
    'this is comments' #docstring:comment of this fuc, can be show using .__doc__
    'this is another,cannot be printed'
    return 'hello, ' + name + '!'
print hello('lili')
print hello.__doc__   # a attribute of fuc
print '----------'
help(hello)           #Help on function hello in module __main__:
                      #hello(name)
                      #this is comments
print '----------'
help(hello('niu'))    #no Python documentation found for 'hello, niu!'
# -1.2- Functions That Aren’t Really Functions
# return nothing (default None)
def notReal():
    print 'return nothing'
    return
    print 'not show'
print '1----------'
notReal()   # return nothing
print '2----------'
print notReal() # return nothing None
# -1.3- formal parameters & actual parameters or arguments
# -1.4- Collecting Parameters
def muti1(*i):
    print i
muti1(1)    #(1,)
muti1(1,2,3)#(1, 2, 3)
def muti2(*i,**j): #cannot have two **j
    print i
    print j
muti2(1,2,3,name='lili',no=2)
# -1.5- scoping
x = 100
scope = vars()
scope['x']+=11
print x   #111
# -1.6- global
x = 100
def change():
    x = 50
    y = 60
    print 'change() x = ',x
    print 'change() global()[x]',globals()['x'] # 100,show global one
    global y   #chang to global,can show outside of this func
    y = y+100
change()
print y
# -1.7- NESTED SCOPES
def out(i):
    def inter(j):
        def inter1(k):
            print('----1')
            return i*j*k
        print('----2')
        return inter1
    print('----3')
    return inter
print out(2)(4)(3) # ---- 3 2 1 24
# -1.8- Recursion(递归)
# -- Factorial
def factorial(n):
    result = n
    for i in range(1,n):
        result *= i
    return result
print factorial(1)
def factorial1(n):
    if n==1:
        return 1
    else:
        return n*factorial1(n-1)
print factorial1(2)
# -- power
def power(x,n):
    if n == 0:
        return 1
    result = 1
    for i in range(n): #range(3)1,2,3;range(1,3)1,2
        result *=x
    return result
print '---',power(2,3)
def power1(x,n):
    if n==0:
        return 1
    else:
        return x*power(x,n-1)
print power1(2,3)
# -1.9- Functional programming
# -- map
# pass all the elements of a sequence through a given function
print map(str,range(10))
value={'01':'lili','02':'lucy'}
value1=[1,2,3]
print map(str,value)    #['02', '01']
print map(str,value1)   #['1', '2', '3']
# -- filter       www.zzzyk.com
# to filter out items based on a Boolean function
def func(x):
    if x >=0: return True
    if x<0 : return False
seq=[-1,0,2,3,-2]
print filter(func,seq) #seq make func return ture;
# -- lambda
# for define 易做图 function,primarily used with map/filter and reduce
print filter(lambda x:x>=0,seq)
# -- reduce
num = [1,2,3,4]
num1 = [1]
print reduce(lambda x,y:x+y,num)
print reduce(lambda x,y:x+y,num1)
 
补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,