Python 为什么dict[';port#u password';][';server#u port';]=126在本例中是正确的?

Python 为什么dict[';port#u password';][';server#u port';]=126在本例中是正确的?,python,Python,这是代码 config={'port_password':{'server_port':123}} config['port_password'] = {} config['port_password']['server_port'] = 126 print(config) config['port\u password']={},为什么config['port\u password']['server\u port']=126正确 #config={'port_password':{'ser

这是代码

config={'port_password':{'server_port':123}}
config['port_password'] = {}
config['port_password']['server_port'] = 126
print(config)
config['port\u password']={},为什么config['port\u password']['server\u port']=126正确

#config={'port_password':{'server_port':123}}
config['port_password'] = {}
config['port_password']['server_port'] = 126
print(config)

当我注释掉第一行时,它说:“名称'config'未定义”。为什么?

这是带注释的代码:

# define a variable "config" referring to a dictionary with one key
# and whose value is another dictionary
config={'port_password':{'server_port':123}}

# lookup the "config" variable and replace its inner dictionary
# with an inner dictionary
config['port_password'] = {}

# look the "config" variable giving a dictionary, then lookup
# "port_password" in that dictionary, giving the inner empty dict
# and set "server_port" to 126 in that inner dict
config['port_password']['server_port'] = 126

# lookup the "config" variable and display the nested dictionaries
print(config)

以下是带有注释的代码:

# define a variable "config" referring to a dictionary with one key
# and whose value is another dictionary
config={'port_password':{'server_port':123}}

# lookup the "config" variable and replace its inner dictionary
# with an inner dictionary
config['port_password'] = {}

# look the "config" variable giving a dictionary, then lookup
# "port_password" in that dictionary, giving the inner empty dict
# and set "server_port" to 126 in that inner dict
config['port_password']['server_port'] = 126

# lookup the "config" variable and display the nested dictionaries
print(config)
执行此操作时:

config = {'port_password':{'server_port':123}}
您定义了一个新的字典
config
,因此该名称存在,并使用值
{'server\u port':123}
设置键
port\u password

如果删除该行,则
config
变量未定义,您将得到
namererror

但是,在下一行,你写道:

config['port_password'] = {}
config['port_password']['server_port'] = 126
因此,与key
port\u password
关联的值现在是一个空字典

当你写作时:

config['port_password'] = {}
config['port_password']['server_port'] = 126
你正在填这本空字典。执行此操作时,可以将密钥
服务器\u端口设置为126。

config = {'port_password':{'server_port':123}}
您定义了一个新的字典
config
,因此该名称存在,并使用值
{'server\u port':123}
设置键
port\u password

如果删除该行,则
config
变量未定义,您将得到
namererror

但是,在下一行,你写道:

config['port_password'] = {}
config['port_password']['server_port'] = 126
因此,与key
port\u password
关联的值现在是一个空字典

当你写作时:

config['port_password'] = {}
config['port_password']['server_port'] = 126

你正在填这本空字典。您可以使用值126设置键
server\u port

第一行创建一个dictionary对象,接下来的两行修改它。如果你注释掉第一行,字典就永远不会被创建,因此没有什么可修改的。如果你只是在第一行上加126,那么第二行和第三行是无点的。第一行创建一个字典对象,接下来的两行修改它。如果你把第一行注释掉,字典就永远不会被创建,因此没有什么可以修改的。如果你把126放在第一行,那么第二行和第三行就没有意义了,非常简洁!撇开话题不谈:我一直在热衷于你在Youtube上的演讲,特别是关于并发性的演讲。感谢所有伟大的内容!非常简洁!撇开话题不谈:我一直在热衷于你在Youtube上的演讲,特别是关于并发性的演讲。感谢所有伟大的内容!