Friday, 26 August 2022

python django, properly use **kwargs in function

 https://www.pythontutorial.net/python-basics/python-kwargs/


1)

def connect(**kwargs):

    print(kwargs)



config = {'server': 'localhost',

        'port': 3306,

        'user': 'root',

        'password': 'Py1thon!Xt12'}


connect(**config)



2)


def fn(*args, **kwargs): print(args) print(kwargs)
Code language: PHP (php)

The fn function can accept a variable number of the positional arguments. Python will pack them as a tuple and assign the tuple to the args argument.

The fn function also accepts a variable number of keyword arguments. Python will pack them as a dictionary and assign the dictionary to the kwargs argument.

For example:

fn(1, 2, x=10, y=20)

Output:

(1, 2) {'x': 10, 'y': 20}

No comments:

Post a Comment