鱼C论坛

 找回密码
 立即注册
查看: 2399|回复: 0

[学习笔记] Python函数嵌套

[复制链接]
发表于 2018-9-14 00:42:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> count=5
>>> def myFun():
        count=22
        print(count)

       
>>> myFun()
22
>>> print(count)
5
>>> def myFun():
        global count
        count=22
        print(count)

       
>>> myFun()
22
>>> print(count)
22
>>> def FunX(x):
        def funY(y):
                return x*y
        return funY

>>> def funX(x):
        def funY(y):
                return x*y
        return funY

>>> funX(2)
<function funX.<locals>.funY at 0x000000F39D8D9D90>
>>> funY(3)
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    funY(3)
NameError: name 'funY' is not defined
>>> funX(2)(3)
6
>>> def funX(x):
        def funY(y):
                return x*y

       
>>> funX(2)(3)
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    funX(2)(3)
TypeError: 'NoneType' object is not callable
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 10:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表