The authors introduce :【 Lonely and cold 】—CSDN High quality creators in the whole stack field 、HDZ Core group members 、 Huawei cloud sharing expert Python Full stack domain bloggers 、CSDN The author of the force program
- This article has been included in Python Full stack series column :《Python Full stack basic tutorial 》
- Popular column recommendation :《Django Framework from the entry to the actual combat 》、《 A series of tutorials from introduction to mastery of crawler 》、《 Reptile advanced 》、《 Front end tutorial series 》、《tornado A dragon + A full version of the project 》.
- This column is for the majority of program users , So that everyone can do Python From entry to mastery , At the same time, there are many exercises , Consolidate learning .
- After subscribing to the column But there are more than 1000 people talking privately Python Full stack communication group ( Teaching by hand , Problem solving ); Join the group to receive Python Full stack tutorial video + Countless computer books : Basics 、Web、 Reptiles 、 Data analysis 、 visualization 、 machine learning 、 Deep learning 、 Artificial intelligence 、 Algorithm 、 Interview questions, etc .
- Join me to learn and make progress , One can walk very fast , A group of people can go further !
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f)
return fs
f1, f2, f3 = count()
>>> f1()
9
>>> f2()
9
>>> f3()
9
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
>>> f1, f2, f3 = count()
>>> f1()
1
>>> f2()
4
>>> f3()
9
# -*- coding: utf-8 -*-
""" __author__ = Xiaoming - Code entities """
def count():
fs = []
for i in range(1, 4):
f = lambda j: (lambda: j * j)
fs.append(f(i))
return fs
f1, f2, f3 = count()
print(f1(), f2(), f3())
The complete examples used are explained below :
import functools
def log(text):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('%s %s():' % (text, func.__name__))
return func(*args, **kw)
return wrapper
return decorator
>>> def now():
... print('2015-3-25')
...
>>> now.__name__
'now'
def log(func):
def wrapper(*args, **kw):
print('call %s():' % func.__name__)
return func(*args, **kw)
return wrapper
@log
def now():
print('2015-3-25')
now = log(now)
>>> now()
call now():
2015-3-25
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('execute')
def now():
print('2015-3-25')
>>> now = log('execute')(now)
The results are as follows :
>>> now()
execute now():
2015-3-25
>>> now.__name__
'wrapper'
Explain that :Python Decorator (decorator) At the time of realization , The decorated function is actually another function ( Function properties such as function name will change , For example, you will find that the function name has changed to wrapper), In order not to affect ,Python Of functools There is a package called wraps Of decorator To eliminate such side effects . Write a decorator When , It's better to add functools Of wrap, It can keep the original function name and function properties
import functools
def log(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('call %s():' % func.__name__)
return func(*args, **kw)
return wrapper
import functools
def log(text):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
print('%s %s():' % (text, func.__name__))
return func(*args, **kw)
return wrapper
return decorator
# -*- coding: utf-8 -*-
""" __author__ = Xiaoming - Code entities """
import functools, time
def metric(fn):
@functools.wraps(fn)
def wrapper(*args, **kw):
start_time = time.time() * 1000
result = fn(*args, **kw)
run_time = time.time() * 1000 - start_time
print('%s executed in %s ms' % (fn.__name__, run_time))
return result
return wrapper
@metric
def fast(x, y):
time.sleep(0.003)
return x + y
@metric
def slow(x, y, z):
time.sleep(0.1257)
return x * y * z
f = fast(11, 22)
s = slow(11, 22, 33)
def int2(x, base=2):
return int(x, base)
>>> import functools
>>> int2 = functools.partial(int, base=2)
>>> int2('1000000')
64
>>> int2('1010101')
85
int2 = functools.partial(int, base=2)
int2('10010')
kw = {
'base': 2 }
int('10010', **kw)
max2 = functools.partial(max, 10)
max2(5, 6, 7)
args = (10, 5, 6, 7)
max(*args)