On Python3 function namespace with a scope

Previous chapter describes the knowledge and namespace scopes, now let's talk about the namespace Python3 function of it.

First, the nature of the function name

The nature of the function name is a hexadecimal address memory function body itself is 变量, it can be said that the function itself is a pointer 指针, as follows:

def hello():
    print("hello")

print(hello)

# 控制台输出:
<function hello at 0xcc485270>

Since it is a variable name on the nature of the function, it can be assigned to other variables, as follows:

def hello():
    print("hello")

a = hello
print(a)

# 控制台输出:
<function hello at 0xcc485270>

Function call, the function name is the English parentheses plus one, i.e. 函数名(), may be understood as 函数地址()follows:

def hello():
    print("hello")

a = hello
print(a)
# 通过变量a调用函数hello()
a()

# 控制台输出:
<function hello at 0xcc485270>
hello

Second, the function namespace

Third, the function scope

Four, global and nonlocal

Published 26 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_35844043/article/details/104074353