您好,我正在编写Python代码,并收到两个带有绿色下划线的变量,分别是“来自外部作用域的影子名称'value'和来自外部作用域的阴影名称'value1'”。这是错误吗?我该如何解决这个问题?我的代码应从Firebase实时数据库读取两个变量数据。如果两个变量数据均为1,则我应该在手机上收到通知。代码是否错误?
请注意,它正常运行,并且能够接收通知,但是当我添加第二个变量并修改了代码后,我不再能够接收通知。
value = 0
value1 = 0
def stream_handler(message):
print(message)
if message['data'] is 1:
value = 1 //here the variable is underlined in green
value = value //here the variable is underlined in green
return value
def stream_handler1(message1):
print(message1)
if message1['data'] is 1:
value1 = 1 //here the variable is underlined in green
value1 = value1 //here the variable is underlined in green
return value1
if value is 1 & value1 is 1:
response = pn_client.publish(
interests=['hello'],
publish_body={
'apns': {
'aps': {
'alert': 'Hello!',
},
},
'fcm': {
'notification': {
'title': 'Notification',
'body': 'Fall Detected !!',
},
},
},
)
print(response['publishId'])
my_stream = db.child("Fall_Detection_Status").stream(stream_handler)
my_stream1 = db.child("Fall_Detection_Status1").stream(stream_handler1)
最佳答案
您可能是这个意思:
def stream_handler(message):
global value
print(message)
# rest of function elided
def stream_handler1(message1):
global value1
print(message1)
# rest of function elided
global
语句告诉Python,您的意思是使用值变量的全局版本,而没有本地版本。另外,您可能不希望这样的语句:
value = value
因为这没有任何意义。
关于python - “来自外部作用域的阴影名称是错误的”吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50295510/