>>> def myfunc(*args, **kwargs):
... print(args)
... print(kwargs)
...
>>> myfunc(1, 2, name="sen")
(1, 2)
{'name': 'sen'}
>>> myfunc(1, 2, name="sen", 3)
File "<stdin>", line 1
myfunc(1, 2, name="sen", 3)
^
SyntaxError: positional argument follows keyword argument
>>> myfunc(name="sen")
()
{'name': 'sen'}
>>> myfunc.__doc__
>>> myfunc.version
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'version'
>>> myfunc.__
myfunc.__annotations__ myfunc.__hash__()
myfunc.__builtins__ myfunc.__init__(
myfunc.__call__( myfunc.__init_subclass__(
myfunc.__class__( myfunc.__kwdefaults__
myfunc.__closure__ myfunc.__le__(
myfunc.__code__ myfunc.__lt__(
myfunc.__defaults__ myfunc.__module__
myfunc.__delattr__( myfunc.__name__
myfunc.__dict__ myfunc.__ne__(
myfunc.__dir__() myfunc.__new__(
myfunc.__doc__ myfunc.__qualname__
myfunc.__eq__( myfunc.__reduce__()
myfunc.__format__( myfunc.__reduce_ex__(
myfunc.__ge__( myfunc.__repr__()
myfunc.__get__( myfunc.__setattr__(
myfunc.__getattribute__( myfunc.__sizeof__()
myfunc.__getstate__() myfunc.__str__()
myfunc.__globals__ myfunc.__subclasshook__(
myfunc.__gt__( myfunc.__type_params__
>>> myfunc.__str__()
'<function myfunc at 0x104ce05e0>'
def repeat(n):
def dec_func(func):
def wrapper(*args, **kwargs):
for _ in range(n):
result = func(*args, **kwargs)
return result
return wrapper
return dec_func
@repeat(3)
def say_hello():
print('hello')
say_hello()
>>> def my_function(*args, **kwargs):
... for arg in args:
... print(f"Position argument: {arg}")
... for key, value in kwargs.items():
... print(f"Keyword argument {key}: {value}")
...
>>> my_function(*(1,2,3,4,5), **{'name':'sen'})
Position argument: 1
Position argument: 2
Position argument: 3
Position argument: 4
Position argument: 5
Keyword argument name: sen
>>> l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> filter(lambda x:True if x > 5 else False, l)
<filter object at 0x102c47370>
>>> list(filter(lambda x:True if x > 5 else False, l))
[6, 7, 8, 9, 10]
>>> list(map(lambda x:x**2, l))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> from functools import reduce
>>> reduce(lambda x,y:x+y, l)
55
# 偏函数 与 柯里化
>>> from operator import add, mul
>>> from functools import partial
>>> add1 = partial(add, 1)
>>> add1(3)
4
>>> mul4 = partial(mul, 4)
>>> mul4(5)
20
>>> base2 = partial(int, base=2)
>>> base2('10010111')
151
局部变量通常会覆盖全局变量,可以在函数中通过global
关键字引用全局变量
>>> def say_hello():
... global foo
... foo = 200
... print(foo)
...
>>> foo = 100
>>> say_hello()
200
>>> foo
200
在 Python 中,闭包是一种函数,它可以访问其外部函数作用域中的变量,即使外部函数已经返回。
闭包由以下几个部分组成:
闭包的作用:
>>> def fibonacci():
... a, b = 0, 1
... def inner():
... nonlocal a, b
... result = a
... a, b = b, a + b
... return result
... return inner
...
>>> fib = fibonacci()
>>> for _ in range(10):
... print(fib())
...
0
1
1
2
3
5
8
13
21
34
>>> fib()
55
生成器函数是一种包含yield关键字的函数。当调用生成器函数时,它不会立即执行函数体,而是返回一个生成器对象。每次调用生成器对象的__next__()方法或在循环中使用生成器时,函数会执行到下一个yield语句,暂停并返回一个值。然后,下次调用__next__()方法时,函数会从上次暂停的地方继续执行。
def my_generator():
for i in range(5):
yield i
gen = my_generator()
for num in gen:
print(num)
在 Python 中,协程是一种轻量级的并发编程方式,它允许在一个线程内实现多个任务的切换和协作,而不需要使用多线程或多进程。协程可以在等待某个事件发生时暂停执行,然后在事件发生后恢复执行,从而实现高效的并发处理.
优点:
>>> def print_received():
... print('started')
... x = yield
... print('received: {}'.format(x))
...
>>> p = print_received()
>>> p.send(None)
started
>>> p.send(10)
received: 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration