1. 程式人生 > >【Python】One interesting way to unlock the package(Tuple & Dict)

【Python】One interesting way to unlock the package(Tuple & Dict)

def unlocker(*args, **kwargs):
    print(*arg)
    for key in kwargs.keys():
        print(key, ":", kwargs[key])

I have to announce that I have grabed something new from the practices:

I found that we should not add "" around the keyword arguments or it will break down, however if we try to write like defining a dict, it will also break down. 

The two wrong ways to assign the arguments and the right way when facing **kwargs:
 

unlocker("arg1", "arg2", "arg3" ,"kwarg1" = 1, "kwarg2" = 2, "kwarg3" = 3) 
# wrong ! It should be like ->    kwarg1 = 1 ,  kwarg2 = 2, kwarg3 = 3

unlocker("arg1", "arg2", "arg3", kwarg1 : 1, kwarg2 : 2, kwarg3 : 3)
# wrong ! It should be like ->    kwarg1 = 1 ,  kwarg2 = 2, kwarg3 = 3

unlocker("arg1", "arg2", "arg3", kwarg1 = 1 ,  kwarg2 = 2, kwarg3 = 3)
#right !

And review another way of visiting the keys and values in dict:

dict_test = {1:"one", 2:"two", "three":3}

for key,value in dict_test.items():
    print(key,":",value)

In the end, remember that we should not add blank space between the keyword like ->   a name = 1,it doesn't work actually.