-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.py
67 lines (50 loc) · 1.29 KB
/
function.py
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
# def power(x, n=2):
# s = 1
# while n > 0:
# n = n - 1
# s = s * x
# return s
# print( power( 2 ) )
# def enroll(name, gender, age=6, city='Beijing'):
# print('name:', name)
# print('gender:', gender)
# print('age:', age)
# print('city:', city)
# enroll('heier', 'F', city='test')
# def add_end(L=[]):
# L.append('END')
# return L
# print( add_end() )
# print( add_end() )
# def add_end(L=None):
# if L is None:
# L = []
# L.append('END')
# return L
# print( add_end() )
# print( add_end() )
# 可变参数
def calc(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum
print( calc(*[1, 2, 3]) )
# 关键字参数
def person(name, age, **kw):
print('name:', name, 'age:', age, 'other:', kw)
person('Michael', 30, city='beijing', test='test')
# 命名关键字参数
def person(name, age, *, city, job):
print(name, age, city, job)
person( 'Heier', '22', city='zhengzhou', job='IT' )
# 使用参数组合
def f1(a, b, c=0, *args, **kw):
print('a =', a, 'b =', b, 'c =', c, 'args =', args, 'kw =', kw)
def f2(a, b, c=0, *, d, **kw):
print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)
f1(1, 2)
f1(1, 2, c=3)
f1(1, 2, 3, 'a', 'b')
f1(1, 2, 3, 'a', 'b', x=99)
f2(1, 2, d=99, ext=None)