python中函数后面的小括号的作用

 

1、

>>> def a():
    print("hello world!")

    
>>> a
<function a at 0x000002CE49DCB550>
>>> a()
hello world!

 

2、

>>> def a():
    print("hello world!")
    def b():
        print("xxxxxx!")
    return b

>>> a()
hello world!
<function a.<locals>.b at 0x0000020074A8F040>
>>> a()()
hello world!
xxxxxx!

 

3、

>>> def a():
    print("hello world!")
    def b():
        print("xxxxxx!")
    return b()

>>> a()
hello world!
xxxxxx!

 

4、

>>> x = 10
>>> def a():
    global x
    x = 1000000
    print(x)

    
>>> a
<function a at 0x000002787AF9B550>
>>> x
10
>>> a()
1000000
>>> x
1000000

小括号的作用表示执行函数。

 

posted @ 2021-03-06 15:57  小鲨鱼2018  阅读(741)  评论(0编辑  收藏  举报