# *args
def eat(*args): # Angle the argument : All location parameters are aggregated into a tuple ; When you define a function ,* For aggregation
print(args) # (' Milk tea ', ' Roasted Duck ', ' Hot pot ', ' Grilled fish ')
print(f' I will treat you to dinner :{
args}') # I will treat you to dinner :(' Milk tea ', ' Roasted Duck ', ' Hot pot ', ' Grilled fish ')
eat(' Milk tea ', ' Roasted Duck ', ' Hot pot ', ' Grilled fish ')
# **kwargs
def func1(**kwargs): # When a function is defined : ** All the keyword parameters of the argument angle are aggregated into a dictionary , And gave kwargs
print(kwargs) # {'name': 'hello', 'age': 17, 'edu': ' Doctor '}
func1(name='hello', age=17, edu=' Doctor ')
# Universal parameters ( Dynamic parameters ):*agrs , **kwargs
def func2(*args, **kwargs):
print(args) # ('hello', 20, 'goto')
print(kwargs) # {'name': 'world', 'age': 17}
func2('hello', 20, 'goto', name='world', age=17)
# * The magic usage of
def func3(*args, **kwargs):
print(args) # ([11, 22, 55, 66, 99], ['today', 'yesterday', 'hello world'])
print(kwargs) # {}
l1 = [11,22,55,66,99]
l2 = ['today', 'yesterday', 'hello world']
func3(l1, l2)
def func4(*args, **kwargs):
print(args) # (11, 22, 55, 66, 99, 'today', 'yesterday', 'hello world')
print(kwargs) # {}
l1 = [11,22,55,66,99]
l2 = ['today', 'yesterday', 'hello world']
func4(*l1, *l2) # When a function is called ,*iterable( Iteratable object ) To break up .
def func5(*args, **kwargs):
print(args) # ({'name': 'hello', 'age': 10}, {'job': 'stu'})
print(kwargs) # {}
func5({
'name':'hello', 'age':10}, {
'job': 'stu'})
def func5(*args, **kwargs):
print(args) # ()
print(kwargs) # {'name': 'hello', 'age': 10, 'job': 'stu'}
func5(**{
'name':'hello', 'age':10}, **{
'job': 'stu'}) # When a function is called ,**dict To break up , At this time, the key of the dictionary cannot be a number .
# * Usage outside of function : Process the remaining elements
a, b, *c = 1,2,3,6,9,99,10
print(a, b, c) # 1 2 [3, 6, 9, 99, 10]
a, *c, b = 1,2,3,6,9,99,10
print(a, c, b) # 1 [2, 3, 6, 9, 99] 10
q, *w = range(1,10)
print(q,w) # 1 [2, 3, 4, 5, 6, 7, 8, 9]
# The order of formal parameter angle parameters : Positional arguments ,*args, Default parameters , **kwargs
def func(a, b, *args, sex=' male ', **kwargs):
print(a) # 1
print(b) # 22
print(args) # (56, 85, 41, 23)
print(sex) # male
print(kwargs) # {'name': 'hello', 'job': 'it'}
func(1,22,56,85,41,23,name='hello',job='it')
# Parameter angle : Key parameters only ( Function calls can only pass parameters through keywords ), be located *args And **kwargs Between
def func(a, b, *args, sex=' male ', age, **kwargs):
print(a) # 1
print(b) # 22
print(args) # (56, 85, 41, 23)
print(sex) # male
print(age) # 77
print(kwargs) # {'name': 'hello', 'job': 'it'}
func(1,22,56,85,41,23,age=77,name='hello',job='it')