forked from techstay/python-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
99 lines (53 loc) · 1.79 KB
/
functions.py
File metadata and controls
99 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
print('--------------简单函数--------------')
def im_a_function():
print("I'm a function")
im_a_function()
print('--------------带参数的函数--------------')
def function_with_param(n=1):
print(f'n={n}')
function_with_param(10)
print('--------------关键字参数--------------')
def function_with_keywords_arguments(a, b, c, d):
print(f'a={a},b={b},c={c},d={d}')
function_with_keywords_arguments(b=1, a=2, d=4, c=3)
print('--------------任意参数列表--------------')
def function_with_varargs(*args):
for arg in args:
print(arg, end=',')
print()
function_with_varargs(1, 2, 34, 'fuck')
print('--------------解包参数列表--------------')
def function_with_unpacking_args(**argMap):
for k, v in argMap.items():
print(f'[{k}:{v}]', end=' ')
print()
function_with_unpacking_args(name='yitian', age=24, gender='male')
args = {'a': 5, 'b': 6, 'c': 1, 'd': 2}
function_with_keywords_arguments(**args)
print('--------------函数注解--------------')
def printFunctionAnnotation(a: str, b: int, c: list) -> None:
print(printFunctionAnnotation.__annotations__)
pass
printFunctionAnnotation('str', 22, [])
print('--------------lambda函数--------------')
im_a_lambda = lambda a, b: a + b
print(f'a+b={im_a_lambda(3,4)}')
print('--------------文档字符串--------------')
def function_with_documents():
'''\
这是一个文档字符串
'''
pass
print(f'文档:{function_with_documents.__doc__}')
print('--------------装饰器--------------')
def surround_decorator(func):
def new_func(*args, **kargs):
print('func_start')
result = func(*args, **kargs)
print('func_end')
return result
return new_func
@surround_decorator
def hello():
print('Hello !')
hello()