如何在Python3中刷新变量



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

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.

当我打印作品时,我的价值良好,但是传感器_name变量没有良好的内容,因为它认为件变量仍然是空的。我该怎么办来解决这个问题?谢谢

有一个外观...

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.

最新更新