Home > Backend Development > Python Tutorial > Detailed examples of the use of python advanced functions

Detailed examples of the use of python advanced functions

零到壹度
Release: 2018-05-15 14:20:23
Original
2490 people have browsed it

This article shares with you a detailed explanation of the use of python advanced functions with examples. The content is quite good. I hope it can help friends in need.

Function parameter problem

##Three basic situations

  • fun(a,b)

  • fun(a,b) b is an iterable object

  • fun(a,**b) b is an iteration object with a retrieval type, inside the function body When parsing, it is quite a dictionary

  • ls = [i for i in range(10)]
    #print(ls)
    def fun1(a,*b):
        for i in b:
            print(a,i)
    #fun1(1,*ls)
    
    def fun2(name,age,**kw):
        print("name:",name,"age:",age,"other:",kw)
    
    fun2('fanhaitao','26',参数= 'random')
    Copy after login
    **kw参数把键值对转换成字典的形式
    Copy after login

Anonymous function

lambda

  1. There is no return value, the return value is the value of the expression

  2. The function has no name, so don’t worry about name conflicts

  3. The anonymous function is also a function object. You can also assign the anonymous function to a variable and use the variable to call the function

  4. fun3 = lambda x:x+1
    print(fun3(99))
    
    #100
    fun4 = lambda x,y :x*x + y*y
    print(fun4(3,4))
    #25
    Copy after login

Decorator@

This method of dynamically adding functions while the code is running is called "Decorator"

Decorator without parameters

#定义一个装饰器
def log(func):
    def wrapper(*args,**kw):
        print('call %s()' % func.__name__)
        return func(*args,**kw)
    return wrapper
@log
def now():
    print('2018-3-29')
    
now()
Copy after login

The internal logical relationship of the decorator (calling sequence): log() -> return wrapper() -> return func() -> now()

Decorator with parameters

#定义一个装饰器
def log(text):
    def decorator(func):
        def wrapper(*args,**kw):
            print('%s %s():' % (text,func.__name__))
            return func(*args,**kw)
        return wrapper
    return decorator
@log("可爱的参数")
def now():
    print('2018-3-29')

now()
Copy after login

BiF built-in function

zip: Combine two iteration objects into one iteration object

Note: redundant unmatched variables will be discarded

a = [1,2,3]
b = 'abcde'
for i in zip(a,b):
    print(i)
for i,j in zip(a,b):
    print("Index:",i,";Item:",j)
Copy after login

enumerate: returns an iterable object, consisting of position + element

for i,j in enumerate('abcde'):
    print(i,j)
Copy after login

filter: filter Function

Two parameters, the first is a parameter, the second is an iterable object, the returned value is also an iterable object; the iterative object in the parameter is True in the function, The value will be retained, otherwise pass

print(list(filter(lambda x:(x*x+x+2)%8 == 0,range(100))))
Copy after login

map

##The usage is similar to filter, the difference is as follows:

  • The function in the parameters operates on each iteration object

  • Returns the operated object

    print(list(map(lambda x:x**2,range(5))))
    Copy after login
reduce

  • In the functools library

  • func receives 2 parameters,

reduce continues the result and performs cumulative calculation with the next element of the sequence

from functools import reduce
add = lambda x,y:x+y
ls = [i for i in range(101)]
print(reduce(add,ls))
Copy after login
#5050


Related recommendations:

Advanced functions of Python

Collection of Python functions and advanced syntax

##Advanced usage of python functions

The above is the detailed content of Detailed examples of the use of python advanced functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template