Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何访问嵌套函数中的实例?_Python_Kivy - Fatal编程技术网

Python 如何访问嵌套函数中的实例?

Python 如何访问嵌套函数中的实例?,python,kivy,Python,Kivy,我正在尝试访问嵌套函数中的值实例。我不知道该怎么办。任何帮助都将不胜感激。多谢各位 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) # rela

我正在尝试访问嵌套函数中的值实例。我不知道该怎么办。任何帮助都将不胜感激。多谢各位

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,则可以在内部函数中将
声明为
非局部变量


for循环中的值?不清楚您到底在问什么。谢谢,但是我如何从函数外部访问它?您可以像在外部函数中一样访问它。@Tim我尝试过不能在嵌套函数外部调用它。您需要首先声明变量
value:int
(在python 3.5+中)或者指定一个伪值:“value=None”,然后调用嵌套函数。在那之后,该值应该可以访问。@Tim我就是这么做的,我仍然不能在父函数之外调用它,我尝试将它赋给一个self值,但它没有返回任何值
def myfunc1():
  x = "John"  # assign x to some dummy variable to be overwritten
  def myfunc2():
    nonlocal x
    x = "hello"
  myfunc2()
  return x

print(myfunc1())