1. Decorator Detailed
Def wrpper (f): #f = func1
def inner (*args,*
Kwargs):
Print (111)
ret = f (Args,**kwargs)
Print (333)
return ret
return inner #返回给wrapper (FUNC1)
@wrpper # func1 = wrapper (func1)
Def func1 ():
Print (222)
Return 444
Func1 () #inner ()
# 1, execute the wrapper function, pass the FUNC1 function name to F
# 2, returns the inner function name to the new variable func1 (func1 = inner)
# 3,func1 () = = Inner () Execute inner function *
#4, execute print (111) to execute the FUNC1 function print (222) to execute print (333)
2. Valid information for functions
######## #没有装饰器
def logger (username,pwd):
"#描述此函数的作用及参数和返回值等信息
This function is a login function and requires username,pwd of two parameters
: Return:true
‘‘‘
Print (111)
Return True
Print (logger. Doc) #查看函数的描述信息
Print (logger. Name) #查看函数的函数名
########## #有装饰器
From Functools Import wraps #1. Introducing Wraps
Def wrpper (f):
@wraps (f) #[email protected] (f)
def inner (*args,*Kwargs):
‘‘‘
Information about the inner function
:p Aram args: Parameters
:p Aram Kwargs: Parameters
: Return:true
‘‘‘
Print (222)
ret = f (Args,**kwargs)
Print (333)
return ret
return inner
@wrpper
def logger (username,pwd):
‘‘‘
This function is a login function and requires username,pwd of two parameters
: Return:true
‘‘‘
Print (111)
Return True
Print (logger. Doc) #输出logger函数信息, if there is no 1 2 operation, the output of the inner function information
Print (logger. Name) #输出logger, if there is no 1 2 operation, the output inner function name
3 Decorator Upgrade
# #带参数的装饰器
def wrpperout (Flag1): #flag1 =flag=true
Def wrpper (f):
def inner (*args,Kwargs):
If Flag1:
Print (0)
ret = f (*args,*Kwargs)
Print (9)
return ret
Else
ret = f (args, Kwargs)
return ret
return inner
Return Wrpper
Flag = True
@wrpperout (flag) #1. Separate @ from the function, execution wrpperout (flag) returns Wrpper 2. Combine @ with Wrpper @wrpper
Def func1 (): Br/>print (111)
@wrpperout (flag)
Print (222)
@wrpperout (flag)
Print (333)
Func1 ()
Func2 ()
FUNC3 ()
# # #多个装饰器装饰一个函数
def wrapper1 (func):
Def inner1 ():
Print (' Wrapper1, before Func ')
Func ()
Print (' Wrapper1, after Func ')
Return Inner1
def wrapper2 (func):
Def inner2 ():
Print (' Wrapper2, before Func ')
Func () # Inner1 ()
Print (' Wrapper2, after Func ')
Return Inner2
def wrapper3 (func):
Def inner3 ():
Print (' Wrapper3, before Func ')
Func ()
Print (' Wrapper3, after Func ')
Return Inner3
@wrapper3br/> @wrapper2
def f ():
Print (' F ')
F ()
Output:
Wrapper3, before Func
Wrapper2, before Func
Wrapper1, before Func
F
Wrapper1, after Func
Wrapper2, after Func
Wrapper3, after Func
Execution order: From top to bottom adorner decoration function before the operation----execution function-----from bottom to top adorner decoration function
4. iterators
# # #可迭代对象: The object contains the Iter method is an iterative object, following the iterative protocol
Print ('iter' in Dir (str)) #判断该对象是不是可迭代对象
From collections Import iterable
Print (Isinstance (' abc ', iterable)) #判断该对象是不是可迭代对象
Print (Isinstance (' abc ', str)) #判断该对象是哪种数据类型
# # #迭代器: The object with the next method containing iter inside is an iterator that follows the iterator protocol
S1 = ' ASGSDG '
obj_s = S1. ITER () #将可迭代对象转换成迭代器或者obj_s = iter (S1)
Print (obj_s.next()) #取值
Print ('next' in Dir (obj_s)) #判断该对象是不是迭代器
From collections Import Iterator
Print (Isinstance (obj_s,iterator)) #判断该对象是不是迭代器
# # # #迭代器的好处: 1. Save memory 2. Inertia mechanism 3. One-way execution, irreversible
5. Generator
The generator is essentially an iterator, and a custom iterator
Def func1 ():
Print (111)
Yield 222 #yield关键字
Yield 333
Yield 444
G_obj = func1 () #生成器对象
Print (g_obj) #<generator object func1 at 0x000001e97f1303b8>
Print (g_obj.next()) #取值,next() and yield one by one correspond
Print (g_obj.send())) #send和next都是对生成器的取值, send sends a value to the previous yield, send cannot be used for the first time, and the last yield cannot get the value
6. List-derived
[Variable (processed variable) for variable in iterable] #遍历模式
[Variable (processed variable) for variable in iterable if condition] #筛选模式
# # #生成器表达式
(Variable (processed variable) for variable in iterable)
(Variable (processed variable) for variable in iterable if condition)
7. Built-in functions
Eval: Remove the quotation marks on both sides
Print (eval (' 1+2+3 ')) #输出6
EXEC: Execute code
S1 = "'
For I in range (5):
Print (i)
‘‘‘
Print (EXEC (S1))
Print (sep= ' delimiter, default = Space ')
Print (sep= ' newline character, default = \ n ')
Print (file=f1w file handle)
Print (hash (' cc ')) #将一个不可变的数据类型转换一个哈希值, if the number is the number itself
Print (Help (str)) #帮助信息
Callable: Checks if an object is callable, callable returns true, non-callable return false
Print (float (1)) #将整数和字符串转换成浮点数, Output 1.0
Print (()) #十进制转换成二进制, Output 0b10010
Print (Oct ())) #将十进制装换成八进制, Output 0o22
Print (Hex ())) #将十进制装换成十六进制, Output 0x12
Print (ABS ( -1)) #abs取绝对值, Output 1
Print (Divmod (100,7)) #商和余数, Output (14, 2)
Print (Round (1.2345,3)) #保留小数的位数, Output 1.234
Print (POW (2,3)) #x的y次幂 if three parameters indicate a Z-fetch
L1 = [A]
Print (sum (L1), 4) #求和, output 10
L1 = [1,2,-3]
Print (min (l1,key=abs)) #取最小值, can add key, Output 1
Print (max (l1,key=abs)) #输出最大值, can add key, output-3
l_obj=reversed (L1) #翻转形成一个迭代器, use for value
REPR: Returns the prototype of an object
Sorted: Can specify key to sort
Enumerate: Enumeration that returns an enumeration object
All: Can iterate objects, all true is true
Any: Can iterate over an object, there is a true, that is true
Zip: Zipper Method
Map
Filter
8, anonymous function
Calc = lambda N -#calc函数名, lambda keyword, n parameter: NN parameter operation * *
Python Fifth day Study summary