函数部分



1、必须有一个明确的结束条件;
2、每次进入一层递归时,问题规模相比上次递归都应有所减少;
---------------------------------------------------------------------------------------

匿名函数总结:
lambda x,y:x+y #return x+y
max(iter,key=lambda)
min(iter,key=lambda)
sorted(iter,key=lambda)
map(lambda,iter)
reduce(lambda,iter)
filter(lambda,iter)




----------------------------------------------------------------------------------
import time
print(time)--------<module 'time' (built-in)>

类似:m=__import__('time')
    print(m)
    m.sleep(3)




filter函数:过滤是TRUE值的内容:

示例:
dic=[{'name':'geon','age':20},
    {'name':'geon2','age':9000},
    {'name':'geon3','age':1000}
    ]
过滤大于100的字典
a=filter(lambda d:d['age']>100,dic)
for i in a:
print(i)








reduce:示例:  

    python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)
    中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是一个二元操作函数)
    先对集合中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,
    最后得到一个结果。

        from functools import reduce

    l=list(range(100))
    print(l)
    reduce(lambda x,y:x+y,l)




---------------------------------------------------------------------------------------

 常用函数:sum   lambda  zip() map


map:示例:

            name_l=['a','b','c']
            map(lambda x:name+'sb',name_l)     <------------map + lambda
                                        <----------map(lambda x:'x--',可迭代对象)

            l=[1,2,3,4,7]

            原实现:    [i**2 for i in l ]
                    print(x)
                      
             map 实现: m=map(lambda i:i**2,l)
                        FOR i  in m:
                            print(i)
                        print(list(m))





     lambda  函数:
            匿名函数
            lambda k: salaries[k]                   <-----------------------
                冒号:后面相当于return 内容。
        salaries={'a':10,'b':20,'c':30}
        f=lambda k:salaries[k]
        print(f)
        print(f('c')) --------------给一个 key  返回 key 对应的 value

        改写 字典取最大值:
        print(max(salaries,lambda k:salaries[k]))        



示例1: 返回字典内最大值的 KEY ,比较 字典内的VALUE:    <------------------


            dic={'a':10,'b':20,'c':30}


            def get_value(k):
                return dic[k]

            print(max(dic,key=get_value))



            lamba K:return(dic[key])

示例2: zip
            print(dic.keys(),dic.values())             
            z=zip(dic.keys(),dic.values())         <----------------------
            print([i for i in z])

            >>>>>
                    dict_keys(['c', 'a', 'b']) dict_values([30, 10, 20])
                    [('c', 30), ('a', 10), ('b', 20)]




----------------------------------------------------------------------------------------------

 声明式编程:



    示例2:  列表嵌套字典

            res=(line.split() for line in f)
            dec=[{'name':j[0],'prince':j[1],'count':j[2]} for j in res]
    
以上可改成:

            res=[]
            with open('b.txt') as f:
                for line in f:
                    l=line.split()
                    d={}
                    d['name']=l[0]
                    d['prince']=l[1]
                    d['count']=l[2]
                    res.append(d)
            print(res)


    示例1:        

                f=open('b.txt')
                ms=(float(line.split()[-1])*float(line.split()[-2]) for line in f)
                sum(ms)

以上可以用生命式替代

            money=[]
            with open('b.txt') as f:
                for line in f:
                    goods=line.split()
                    res=int(goods[-1])*int(goods[-2])
                    money.append(res)
                    # prin
            print(sum(money))



    
    sum()  可以plus 可迭代对象;         ------------------------------
    
如:sum([1,2,3,4])
        numJ_g=(i for i in range(3))
        print(sum(numj_g))


    
------------------------------------------------------------------------------------

    f=open('sp')
    # l1=[i.strip() for i in f]
    l1=(i.strip() for i in f)                <------------------ 可迭代对象;
    print(next(l1))            
      a=list(l1)                                <------------- list() 括号可以包含可迭代对象 ,生成列表;


    g=l=('egg%s'%i for i in range(100))      元组 ---生成器;
    print(g)
    print(next(g))

                >>> i=('egg %s'%i for i in range(100) if i<50)
                >>> print(i)
                <generator object <genexpr> at 0x7ff0e56b7f10>
                >>> print(next(i))
                egg 0
                >>>

                    -----------------------------------------------------------------------


    列表表达式;



    go=os.walk('/home/zjf')
    l1=['%s/%s'%(i[0],j) for i in g for j in i[-1]]
    print(l1)


    i=['egg %s'%i for i in range(100) if i > 50]

    l=[1,2,3,4]
    s='hello'
    l1=[(num,s1) for num in l for s1 in s]
    print(l1)


------------------------------------------------------------------------------------


    面向过程函数示例:




import os

def init(func):
    def wrapper(*args,**kwargs):
        res = func(*args,**kwargs)           #!!!!!!!!!!!!!!!!!! 忘记加参数
        next(res)
        return res
    return wrapper                        #!!!!!!!!!!!!!!!!!!!!!!忘记返回wrapper

@init
def search(target):
    while True:
        dir_name = yield
        g = os.walk(dir_name)
        for i in g:
            for j in i[-1]:
                file_path = '%s/%s'%(i[0],j)
                target.send(file_path)

@init
def opener(target):
    '打开文件获取文件句柄'
    while True:
        file_path=yield
        with open(file_path) as f:
            target.send((file_path,f))   #!!!!!!!!!!!!!!   传2个参数
@init
def cat(target):
    '读取文件内容'
    while True:
        file_path,f=yield         #!!!!!!!!!!!!!!!!!!!!!接收2个参数
        for line in f:
            target.send((file_path,line))
@init
def grep(pattern,target):   #!!!!!!!!!!!!!!!    先参数,后函数target
     '过滤行内容有无python'
     while True:
         file_path,line=yield
         if pattern in line:
             target.send(file_path)
@init
def printer():
    '打印文件路径'
    while True:
        file_path=yield
        print(file_path)

g=search(opener(cat(grep('python',printer()))))
g.send('/home/zjf/geon')








协程函数:
----------------------------------------------------------------------------------

   示例:作业:  连续输入网址打印结果;

                from urllib.request import urlopen
                def get():
                    while True:
                        url=yield
                        res=urlopen(url).read()
                        print(res)
                e=get()
                next(e)
                print(e.send('http://www.baidu.com'))
                print(e.send('http://www.sina.com'))




----------------------------------------------------------------------------------
协程函数:(使用yield 表达式的生成器)

        yield 表达式:
            开始的生成器不能使用send,    
            必须先netx()之后才能使用send()

        #e.send与next(e) 的区别:
        1、如果函数内yield是表达式形式,那么必须先next(e)
        2、二者共同之处都是可以让函数在上次暂停的位置继续运行;
            不一样:send在触发下一次代码的执行时,会顺便给yield赋值;



            示例1、:刚蛋吃包子


        def eater(name):
            print('%s start to eat'%name)
            while True:
                food=yield             #类比: 去饭馆点菜以后等着。 print('%s start to eat'%name)
    while True:
        food=yield             #类比: 去饭馆点菜以后等着。
        print('%s get %s,to start eat'%(name,food))
    print('done')

e=eater('刚蛋')
print(e)
print(next(e))
e.send('包子')
                print('%s get %s,to start eat'%(name,food))
            print('done')

        e=eater('刚蛋')
        print(e)
        print(next(e))
        e.send('包子')            #send(self,value) yield = '包子'  send 相当于next()


            示例2:
                    def eater(name):
                        print('%s start to eat'%name)
                        foodlist=[]
                        while True:
                            food=yield foodlist
                            print('%s get %s,to start eat'%(name,food))
                            foodlist.append(food)
                        print('done')

                    e=eater('刚蛋')
                    # print(e)
                    print(next(e))
                    print(e.send('包子'))



        示例3: 跳过 next(e)


                def fst(fun):
                    def wrapper(*args,**kwargs):
                        # fun(**args)
                        res=fun(*args,**kwargs)
                        next(res)
                        return res
                    return wrapper



                @fst                                      # ester=fst(eater)
                def eater(name):
                    print('%s start to eat'%name)
                    foodlist=['动骨']
                    while True:
                        food=yield foodlist
                        # print('get %s,to start eat'%food)
                        foodlist.append(food)
                    print('done')



                e=eater('刚蛋')                          #wrapper('刚带')
                # print(next(e))
                print(e.send('包子'))
                print(e.send('包子2'))
                print(e.send('包子3'))
                print(e.send('包子4'))
                print(e.send('包子5'))
                print(e.send('包子6'))









----------------------------------------------------------------------------------
示例2、:tail管道 程序用python编写; tail |grep 'error'
                import time
                def tail(file_path):
                    with open(file_path,'r') as f:
                        f.seek(0,2)
                        while True:
                            line=f.readline()
                            if not line:
                                time.sleep(0.3)
                                continue
                            else:
                                yield line

                def grep2(pattern,lines):
                    for line in lines:
                        if pattern in line:
                            print(line)

                #调用
                g=tail('/tmp/a.txt')       #带参数的函数作为 另一个函数的输入!!!
                grep2('error',g)



示例1、:tail程序用python编写;
 

            #/usr/bin
            import time
            def tail(file_path):
                with open(file_path,'r') as f:
                    f.seek(0,2)
                    while True:
                        line=f.readline()
                        if not line:
                            time.sleep(0.3)
                            continue
                        else:
                            print(line,end='')


            tail('/tmp/a.txt')



------------------------------------------------------------------------------------------



                            生成器





与return区别:
生成器就是一个函数,这个函数内包含yield这个关键字;    <-------------------------!!!!!
return只能返回一次,函数彻底结束;多个yield能返回多次值;
生成器的本质就是迭代器;              <----------------------------------!!!!!!!!!!!

可迭代的数据类型: 可以用for循环的 就是可迭代的;   <-----------------------!!!!!!!!!!


yield干了什么:
        1、把函数变成生成器--->  迭代器     g=test() g.__iter()  g.__next()
            #用RETURN 返回值只能返回一次,而YIELD返回多次
            #函数在暂停以及下一次继续喜下一次运行的状态是有YIELD保存;
yield功能总结;
    1、相当于把__iter__和__next__方法封装到函数内部
    2、与RETURN比,RETUN只能返回一次,而YIELD返回多次;
    3、函数暂停已继续运行的状态通过yield保存



                for i in g:
                    print(i)

        示例:


            def a(n):
                print('cut down:')
                while n > 0:
        
                    yield n
                    n -=1

            g=a(5)

            #for i in g:



                for i in g:
                    print(i)

        示例:


            def a(n):
                print('cut down:')
                while n > 0:
        
                    yield n
                    n -=1

            g=a(5)

            #for i in g:
            #    print(i)
            
            或者:

            while True:
                try:
                    print(next(g))
                except StopIteration:
                    break

--------------------------------------------------------------------------------

            def test():
                print('first')
                yield 1  #return 1       碰到yield 停止,并返回 1
                yield 2
            test()
            g=gest()
            print(isinstance(g,Iterator))  是否是可迭代对象;        
            print(next(g))                    触发对象是函数;


    示例:
                
                    def scq():
                        print('a')
                        yield 1,2,3,4
                        print('b')
                        yield 2
                        print('c')
                        yield 3

                    res_sc=scq()
                    res=next(res_sc)
                    print(res)
                    res=next(res_sc)
                    print(res)
                    res=next(res_sc)


----------------------------------------------------------------------------------
isinstance('ssssss',str)
type('ssss') is str >>>>>>>>> True
可迭代的数据类型: 可以用for循环的 就是可迭代的;
数据类型是迭代器:  只有文件本身是迭代器;

                       迭代器  (优缺点)
                            

        1、    提供了一种不依赖索引取值的方式;这样就可以遍历没有索引的可迭代对象:

                如: 字典,集合,文件
        2、 迭代器与列表比较,迭代器更省内存;(惰性计算;)
        3、 缺点:无法获取迭代器长度;使用不如列表索引取值灵活;
                  迭代器一次性取值;

            d={'a':1,'b':2,'c':3}
            i=iter(d)
            while True:
                try:

                    print(next(i))
                except StopIteration:    # 类似if StopIteration:
                    break



#可迭代的: 只要对象本身有__iter__()方法,那它就是可迭代的;

d={'a':1,'b':2,'c':3}

i=d.__iter__()
print(i.__next__())

______________________________________________________________________________
把装饰器内的参数包到函数里面; @timmer(auth_type=file)    有参装饰器!


示例:            


        装饰器: 最重要的是定义wrapper不能少了return值;!!!!!!!!!!!!!!!!!!



            def auth2(auth_type):
                def auth(fun):
                    print(auth_type)
                    def wrapper(*args,**kwargs):
                        if auth_type=='file':
                            name=input('username:')
                            password=input('password:')
                            if name=='zhejiangf4' and password=='sb':
                                print('auth successful')
                                res=fun(*args,**kwargs)
                                return res
                            else:
                                print('auth error')
                    return wrapper
                return auth


            @auth2(auth_type='file')
            def index():
                print('welcome to index page !')

            index()


补充:
@ccchttps://www.baidu.com/
@bbb
@aaa
def fun():
    print('aaa')

fun=ccc(bbb(aaa(fun)))

示例3:
@ccc('c')
@bbb('b')
@aaa('a')
def func():
    pass

func=ccc('c')(bbb('b')(aaa('a')(func)))

______________________________________________________________________________
#装饰器

import time
def timmer(fun):
    def wrapper():
        start_time=time.time()
        func()                                -------------->index()
        stop_time=time.time()
        s_time=stop_time-start_time
        print('pro runs time is %s'%s_time)
    return wrapper

                @装饰器语法   timmer名称
@timmer            #-------->  index=timmer(inex)=wrapper    !!!!!!!!!!!!注意:是index名字!
def index():
    time.sleep(3)
    print('welcme to old boy')

index()          index=wrapper    -------------------wrapper()

    执行过程:----------1、先找到index  ---index地址,2、执行 ()


______________________________________________________________________________





闭包:首先必须是内部定义的函数,包含对外部作用域,而非全局作用域名字的引用;

                        x=1
                        def f1():
                            x=2
                            def f2():
                                print(x)
                            return f2            # f2!

                        f=f1()                    #  --->内部f2
                        f()
                        print(f.__closure__)    # 闭包有__closure__
                        print(f.__clousure__[0].cell_contents)

例子2:
                        x=1
                        def f1(x):
                            # x=2
                            def f2():
                                print(x)
                            return f2

                        f=f1(1000000000)
                        f()
                        print(f.__closure__)
                        print(f.__closure__[0].cell_contents)

    
            zjf@zjf-MATE:~/test$ python3 a.py
            1000000000
            (<cell at 0x7f9158116468: int object at 0x7f915811e710>,)
            1000000000

例子3:
    from urllib.request import urlopen    
    
                def get(url):
                    def index():
                        return urlopen(url).read()
                    return inex
                

f=get('http://www.baidu.com')
print(f.__closure__[0].cell_contents)
                    
                # 下载www.baidu页面


                

______________________________________________________________________________

功能(def),主逻辑(if ....);---------
函数可以看成变量:函数可以被赋值;可以被当作参数传递;


def foo():
    print('foo')

def bar(func):
    print(func)            #打印 foo 内存地址;
    fun()                #执行 foo  函数;

bar(foo)




_______________________________________________________________________________

x=1
def foo():
    x=1
    print(x)

foo()

        作用域: 函数内部---->函数外部-----> 内置名称






_______________________________________________________________________________

            参数位置顺序:  (x,*args,**kwargs)

            **kwargs   字典方式; 除位置参数意外剩下的。
            eg:
                    def foo(x,*args,**kwargs):
                        print(x)
                        print(args)
                        print(kwargs)
                    foo(1,y=1,z=2)      y=1,z=2 传给kwargs


    >>> foo(3,'a'=2)
      File "<stdin>", line 1
      SyntaxError: keyword can't be an expression


                
        def foo(x,**kwargs):
            print(x)
            print(kwargs)
        
        foo(1,y=2,a=3,b=4)            # **kwagrs实参索引不能加 '' !!!!!!!!!!!!!!

>>>>>>1
>>>>>>{'y':2,'a':3,'b':4}



__________________________________________________________________________________
                *args        #args 位置参数;

                示例1:        
                        def foo(x,*args):
                            print(x)
                            print(args)
                        foo(1,2,3,4,5,6,'a','b')   *args 实参元组格式必须加 ''
                示例2:
                        def bar(x,y,z):                    #需要一一对应;(与*(值))
                            print(x)
                            print(y)
                            print(z)
                        bar(*(1,2,3))                   #  *(1,2,3) 类似1,2,3 = bar(1,2,3)
                        多出的以元组的形式给args

                    sum([1,2,3,4])

            
        tips:
            1、位置参数特点: 必须传值的参数!
            2、默认参数必须放在位置参数后面!
            3、是参是可变类型 结果会受函数的影响  (编程可控性变差):  不可变类型不会;

                        def bar(x):
                            x.append(4)

                        x=[1,2,3,]
                        bar(x)               # x传递的是内存地址;
                        print(x)
                        >>>>>[1,2,3,4]



__________________________________________________________________________________
head,*_=[1,2,3,4,5]
>>>head
>>>1                                对于元组,序列类型都可以这么玩;

a,b,c,d,e=[1,2,3,4,5]
a,_,_,_,e=[1,2,3,4,5]
>>>a
>>>1
>>>e
>>>5

x='hello'
x,y,z,x1,x2='hello'
x='h'
y='e'
z='l'
x1='l'
x2='9'

                    例子1:
                            def bar(x,y):
                                return 1,2,3            # 多值return

                            a,b,c=bar(1,2)

                            print(a)
                            print(b)
                            print(c)

                    例子2:
    
                            def foo(x,y):
                                print(x)
                                print(y)

                            a=100
                            b=200
                            foo(a,b)    #是参必须是明确的值

                    例子3:        
                                
                            x=[]            #每次改全局的值;
                            def register(name,name_list=x):
                                name_list.append(name)

                            register('ASB')
                            register('ysb')
__________________________________________________________________________________

def bar(x,y):return 1,2,3,4,[1,2],{'a',1}
                                     多值返回,格式为元组;
>>>>(1, 2, 3, 4, [1, 2], {1, 'a'})

_______________________________________________
def bar(x,y):
    res=x+y
    return res

res1=bar(1,2)
res=bar(1,2)*10
bar(bar(1,2),3)



def 函数名(arg1,arg2,arg3):
    '描述信息'
    函数体
    return 1 (可以是任意数据类型;)

无参函数一般不需要返回值;

                                            

____________________________________________________________________________________

硬盘保存数据--------->(完全读到内存;) 其实就是覆盖原来的数据;

with open('a.txt') as f, open('b.txt') as f2:
    pass

f.seek(0)   光标到开头,truncate(3) 从开头截断到第3个位置;其他丢弃;
truncate(3)

with open('a.txt','w') as f:
    f.writelines(['111111111 ','22222222222 ')
    f.seek(0)
    f.truncate(3)


with open('a.txt','w') as f:
    f.writelines(['111111111 ','22222222222 ')
eval() 字符串转字典,列表;

原文地址:https://www.cnblogs.com/santizhou/p/7639612.html