如何在python3中刷新变量

时间:2019-03-20 07:43:41

标签: python-3.x

我有一些这样声明的全局变量:

piece = ''
sensor_name = 'ID_' + piece + '-'

我的主要功能如下:

if __main__ == "__main__":
     global piece, sensor_name
     piece = "value"
     print(piece) => show "value", it's ok
     print(sensor_name) => show "ID_-" and that's all.

当我打印件时,我具有很好的价值,但是sensor_name变量没有好的内容,因为它认为件变量仍然为空。 我该怎么做才能解决这个问题? 谢谢

1 个答案:

答案 0 :(得分:0)

看看...

piece = ''
sensor_name = 'ID_' + piece + '-'   #variable sensor_name interperted here only ans assigned a value of 'ID_-'

if __name__ == "__main__":    #__main__ should be __name__
     global piece, sensor_name   #Yes you can use variable with global to any function in file/class
     piece = "value"
     sensor_name = 'ID_' + piece + '-' #place this code here to have desired output
     print(piece) => show "value", it's ok
     print(sensor_name) => show "ID_-" and that's all.