Skip to content

Latest commit

 

History

History
95 lines (33 loc) · 819 Bytes

参数 传参 可变参数.md

File metadata and controls

95 lines (33 loc) · 819 Bytes

'''

位置参数(positional argument) 必须传入

默认参数 可传或者不传就用默认参数

可变参数

---

*args 是一个 tuple, 变量名 args 指向这个 tuple 对象

**kwargs 是一个 dict, 变量名 kwargs 指向这个 dict 对象

---

关键词参数(keyword argument)

'''

def foo(first, *args, **kwargs):

print(first)

print(args)

print(kwargs)

# foo(1, 3, a=1)

'''

'''

def foo1(a, second = 'hello'):

print(a, second)

foo1(3, second='123')

def foo2(first, second):

print(first, second)

str = ['hello', 'world']

# foo2(*str)

# print(*str) # 拆开了 str

def foo(k, *args, **kwargs):

print(k)

print(args)

print(kwargs)

embed_kwargs = {'a': 1, 'b': 2}

def foo2(**kwargs):

print(kwargs)

foo2(global_step=2)