luyantl 发表于 2018-9-14 00:42:07

Python函数嵌套

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) 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
>>>
页: [1]
查看完整版本: Python函数嵌套