Key words arguments in python function

This is a problem of how to use *args and **kwargs

Just suplement/add a way for defining the default value of arguments that is not assigned in key words when calling the function:

def func(**keywargs):
    if 'my_word' not in keywargs:
        word = 'default_msg'
        print(word)
    else:
        word = keywargs['my_word']
        print(word)

call this by:

func()
func(my_word='love')

you'll get:

default_msg
love

read more about *args and **kwargs in python: https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3

猜你喜欢

转载自www.cnblogs.com/sonictl/p/11486711.html