# 方法一:
>> servicePort = ''.join(['config.',serviceName,'Port'])
>>port = locals()[servicePort)]
>>print "%s:%d" %(serviceName, port)
# 输出结果
service1Port:8001
service2Port:8002
service3Port:8003
# 方法二:
>> servicePort = ''.join(['config.',serviceName,'Port'])
>>port = vars()[servicePort)]
>>print "%s:%d" %(serviceName, port)
# 输出结果
service1Port:8001
service2Port:8002
service3Port:8003
# 方法三:
>> servicePort = ''.join(['config.',serviceName,'Port'])
>>port = eval(servicePort)
>>print "%s:%d" %(serviceName, port)
# 输出结果
service1Port:8001
service2Port:8002
service3Port:8003
这里我想通过这个例子说明如何在python3中将变量设置为变量名,如何将字符串设置为变量名
稍后会详细写出来
如何得到变量类型
type("11")