如何在python3中刷新变量

问题描述 投票:-1回答:1

我有一些全局变量声明如下:

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变量没有得到好的内容,因为它认为件变量仍然是空的。我该怎么做才能解决这个问题?谢谢

python-3.x
1个回答
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.
© www.soinside.com 2019 - 2024. All rights reserved.