zhan zhan - 3 years ago 209
Python Question

why could dict.get('',None) return a list in Python?

I am reading a sourse code and get some doubt.The 'config' here is a dict.

server_port = config.get('server_port', None)

why could config.get('server_port', None) return a list ?

if server_port:
if type(server_port) == list:
for a_server_port in server_port:
config['port_password'][a_server_port] = config['password']
config['port_password'][str(server_port)] = config['password']

Answer Source

Because "server_port" is the key for a list that is stored in the dict.

The "None" just specifies a default value in the case of a non-existent key- value pair.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download