Convenient argument passing technique in Python (tuple, dictionary type)

A handy trick when passing arguments to a function in Python

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 argumentsKey"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.

Summary

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

Convenient argument passing technique in Python (tuple, dictionary type)
Function argument type definition in python
Get multiple maximum keys in Python dictionary type
Create a dictionary in Python
Avoid KeyError in python dictionary
Expansion by argument of python dictionary
Python for super beginners Python # dictionary type 1 for super beginners
Dynamically load json type in python
Type specified in python. Throw exceptions
Python for super beginners Python # dictionary type 2 for super beginners
Specify your own class in class argument and return type annotation in Python
Study from Python Hour6: Frequently used data types: tuple type, set type, dictionary type
[Python] Easy argument type check with dataclass
[Introduction to Udemy Python3 + Application] 21. Tuple type
Python / dictionary> setdefault ()> Add if not in dictionary
Hash in Perl is a dictionary in Python
[Introduction to Udemy Python3 + Application] 24. Dictionary type
Python technique
Dictionary type 2
Dictionary type 1
Python dictionary
[Python] dictionary
Python> zip (* list4)> Matrix conversion> List and tuple argument expansion / dictionary argument expansion / local variable dictionary locals ()
Python dictionary
Basic operation list of Python3 list, tuple, dictionary, set
Static type checking that starts loosely in Python
I compared "python dictionary type" and "excel function"
How to get dictionary type elements of Python 2.7
How to handle datetime type in python sqlite3
I checked the reference speed when using python list, dictionary, and set type in.
Get the value of a specific key in a list from the dictionary type in the list with Python