Positional parameters and keyword arguments in python

1. variables, parameters, naming

We variable naming, to avoid keyword python when the function name, so how to get a list of keywords python do? We can not always forget all go to Baidu, by the following method, you can view:

import keyword
keyword.kwlist

After running the command returns a list that contains keywords, a total of 35.

About naming can refer PEP8 specification:

https://www.python.org/dev/peps/pep-0008/#naming-conventions

2. random location parameters and random keyword arguments

Arbitrary Positional Arguments (parameter random location) and Arbitrary Keyword Arguments (arbitrary keyword arguments).

def do_something(a, b, *args, **kwargs):
    print(a)
    print(b)
    print("args:", args)
    print("kwargs:", kwargs)
    
do_something(1, 2)
print("====================")
do_something(1, 2, 3, 4, 5)
print("====================")
do_something(1, 2, 3, 4, 5, age=18, gender="f")

After you can see the position of the parameters passed in, be treated as a tuple, keyword arguments passed in, are treated as dictionary.

So that you can define a tuple and dictionary, and then passed as a parameter, but to add the tuple before *, ** plus former Dictionary:

 

Guess you like

Origin www.cnblogs.com/yunxiaofei/p/11203972.html