开发者社区> 问答> 正文

python在函数中定义函数

python在函数中定义函数

展开
收起
montos 2020-04-16 18:42:13 511 0
1 条回答
写回答
取消 提交回答
  • 刚才那些就是函数的基本知识了。我们来让你的知识更进一步。在Python中我们可以在一个函数中定义另一个函数:

    def hi(name="yasoob"):
        print("now you are inside the hi() function")
    
        def greet():
            return "now you are in the greet() function"
    
        def welcome():
            return "now you are in the welcome() function"
    
        print(greet())
        print(welcome())
        print("now you are back in the hi() function")
    
    hi()
    #output:now you are inside the hi() function
    #       now you are in the greet() function
    #       now you are in the welcome() function
    #       now you are back in the hi() function
    
    # 上面展示了无论何时你调用hi(), greet()和welcome()将会同时被调用。
    # 然后greet()和welcome()函数在hi()函数之外是不能访问的,比如:
    
    greet()
    #outputs: NameError: name 'greet' is not defined
    

    那现在我们知道了可以在函数中定义另外的函数。也就是说:我们可以创建嵌套的函数。现在你需要再多学一点,就是函数也能返回函数。

    2020-04-16 18:42:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载