Variadic and keyword arguments

1. Variable parameters, represented by *arg. When the number of incoming parameters is uncertain, variable parameters can be used.

def test(*a):
sum = 1
for x in a:
sum *= x
return sum

def main():
print(test(1,2))
print(test(1,2,3))
print(test(1,2,3,4))

if name = ‘main‘:
main()

2. Keyword parameters, represented by *kw. The keyword parameter is increased by 2 , and the returned result is a dictionary. Keyword arguments can also be any number.

def person(name, gender, city='Chengdu', **others): # The first 2 are mandatory parameters, the third is the default parameter
print('name:', name, ' gender:', gender, ' City:', city, ' Other information:', others)

person('Han Meimei', 'male', 'Chengdu')
person('Han Meimei', 'male', age='18', occupation='old driver', appearance='super handsome')
extra = {'age' : '18', 'occupation': 'old driver', 'appearance': 'super handsome'}
person('Han Meimei', 'male', **extra)
a = 'Han Meimei', 'male', 'Chengdu'
b = {'Age': '18', 'Occupation': 'Old driver', 'Appearance': 'Super handsome'}
person(*a, **b) # Variable parameters must be called through tuples and dictionaries and keyword arguments

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325505112&siteId=291194637