python - 如何访问嵌套函数中的实例?
问题描述
我试图访问嵌套函数中的值实例。不知道该怎么做。任何帮助,将不胜感激。谢谢
def on_pre_enter(self, *args):
notifications_screen = self.manager.get_screen('notif')
def listener(event):
print(event.event_type) # can be 'put' or 'patch'
print(event.path) # relative to the reference, it seems
print(event.data) # new data at /reference/event.path. None if deleted
notifications = event.data
if notifications.items() == None:
return
else:
for key, value in notifications.items():
print()
notifications_screen.notificationslist.adapter.data.extend([value])
解决方案
如果您使用的是 python3,您可以在内部函数中声明value
为变量。nonlocal
def myfunc1():
x = "John" # assign x to some dummy variable to be overwritten
def myfunc2():
nonlocal x
x = "hello"
myfunc2()
return x
print(myfunc1())
推荐阅读
- css - 如何隐藏具有相同类的第三个元素
- javascript - javascript - 生成并打印所有学生的总平均最终成绩
- excel - 如何在excel中显示总和最大的人的名字?
- python - 使用 Python... 转到该站点,检查一个新的 zip 文件,然后
- python - 如何将此循环的结果写入文本文件?
- spring - Spring JPA 锁定概念
- c# - Hangfire:在 postgres 上打开了太多连接
- gradle - 从子项目调用任务时如何从根gradle任务引用子项目目录
- c++ - 如何在 Windows 上创建 AMQP-CPP 事件循环?
- php - 如何延长脚本执行时间并解决mysql“发送QUERY数据包时出错。”?