python函数嵌套函数

def func():

  print("helloworld")

  global func1

  def func1():

    print("hello world")

func()

func1()

执行:

hello helo
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\ibm\myfile2.py", line 8, in <module>
    func1()
NameError: name 'func1' is not defined
[Finished in 0.2s]

 

解决思路1:提前声明

def func1():
    pass
def func():
    print("hello helo")
    global func1
    def func1():
        print("heloworld")

func()
func1()

解决思路2:作为返回值

def func():
    print("hello helo")
    def func1():
        print("heloworld")
    return func1

myfun=func()
myfun()

 

posted on 2016-09-15 00:18  enet01  阅读(89)  评论(0编辑  收藏  举报