Assuming you are sending a string, when you send an argument to the function The method of receiving by variable, tuple type, dictionary type is as follows.
function.py
# ****************
# * 2020.07.08
# * ProOJI
# ****************
def menu(food, *args, **kwargs):
print(food)
#Tuple
print(args)
print('args[0] = ' + args[0])
print('args[1] = ' + args[1])
#Dictionary type
print(kwargs)
#It is also possible to output Key and Value separately
for k,v in kwargs.items():
print('Key is '+k+'. Value is '+v+ '.')
menu('Apple', 'Banana', 'Orange', entree='Beef', drink='Beer')
Since the first argument 'Apple'
is received by the food
of the function menu
print(food) #Output: Apple
It will be.
Next, there is a * args
with one asterisk on the function menu
side.
It takes multiple arguments as tuples. It is as follows.
print(args) #output:('Banana', 'Orange')
Tuples can also be treated like arrays It is also possible to pick up the value as follows.
print('args[0] = ' + args[0])
#Output: args[0] = Banana
print('args[1] = ' + args[1])
#Output: args[1] = Orange
Finally, there is a ** kwargs
with two asterisks on the function menu
side.
It can be received in dictionary form.
I'm sending ʻentree ='Beef', drink ='Beer'as arguments
Key"entree": Value"Beef"
Key" drink ": Receives in a dictionary type of the form Value" Beer ". As a result, if you normally output with
print ()`
python
print(kwargs) #output:{'entree': 'Beef', 'drink': 'Beer'}
However, get each by for k, v in kwargs.items ():
python
for k,v in kwargs.items():
print('Key is '+k+'. Value is '+v+ '.')
#output
# Key is entree. Value is Beef.
# Key is drink. Value is Beer.
You can also.
There are various ways to pass functions by value, and you can use them conveniently depending on your ingenuity. Python is used in conjunction with various frameworks and libraries I want to keep the basics down.
Recommended Posts