python_ unpack

1, directly to unpack tuple, list, parameter passing sequence collection, of course, also possible to pass the parameter string, if not the key = value formats can be

 
In addition: a collection is unordered, the best way not to use collection
 
NOTE: The number of unpacking a number of consistency with parameter passing
# Unpack --list, tuple, set 
DEF Connect (IP, Port, username, password):
     Print (IP)
     Print (Port)
     Print (username)
     Print (password)

info_list=['192.168.1.1',3309,'zhaozhao','123456']
info_tuple = ( ' 192.168.1.1 ' , 3309, ' zhaozhao ' , ' 123456 ' )
info_set={'192.168.1.1',3309,'zhaozhao','123456'}

connect(*info_list)
connect(*info_tuple)
connect(*info_set)

2, the dictionary mode unpacking

* Number two in this way can be used as a dictionary of mass participation
 
Also, such a parameter passing a dictionary key, and the function parameters must be the same name
dic={"name":"zhaozhao","password":"123456"}

def dic_fun(name,password):
    print(name)
    print(password)

dic_fun(**dic)


/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/dongyf/Documents/python/besttest_study/test.py
zhaozhao
123456

 

Guess you like

Origin www.cnblogs.com/xiaokuangnvhai/p/11074634.html