From function object to the namespace and scope

Function object

As an integral part of the programming language function (Function).

All is in Python object function is no exception, as a function of a variable can be assigned to an object, the object can be added to the collection as an element, can be passed as a parameter value to other functions, may also be used as the return value of the function, these characteristics the first class object is unique.

Object function has three common attributes: value, id, type

def pr_Hi():
    print('Hello World!')

print(id(foo))  # 21301264
print(type(foo))  # <class 'function'>
print(foo)  # <function pr_Hi at 0x01450810>

Functions can be referenced

def pr_Hi():
    print('Hello World!')

pr_Hi2 = pr_Hi    #引用,赋值
print(pr_Hi2)  # <function pr_Hi at 0x03020810>
print(pr_Hi)  #<function pr_Hi at 0x03020810>
pr_Hi2()  # Hello World!

Function can be put into a container class data type

Container object (list, dict, set, etc.) can be stored in any object, functions can also be stored in the container object

def pr_Hi():
    print('from pr_Hi')
dic={'func':pr_Hi}

pr_Hi()  # from pr_Hi

print(dic['func'])  # <function pr_Hi at 0x00BE0810>

dic['func']()  # from pr_Hi

It can also function as a parameter

def pr_Hi():
    print('from pr_Hi')  # from pr_Hi

def bar(func):  ##执行函数
    print(func)  # <function pr_Hi at 0x014E0810>
    func()  ##调用上面地址的函数

bar(pr_Hi)  ##第一步,先调用bar()函数

It can function as a return value

def pr_Hi():
    print('from pr_Hi')

def bar(func):
    return func     

f=bar(pr_Hi)

print(f)  # <function pr_Hi at 0x00CA0810>

f()  # from pr_Hi

Functions can also be nested

def f1():
    def f2():
        print('from f2')
    return f2

abc = f1()  # f1()拿到函数的返回值,函数的返回值是f2, abc就相当于f2
abc()  # from f2

Function space and scope

Nesting a function of involuntary side-tracked to start pulling the scope and function space.

Function space: a place to store names, to be exact namespace is a place to store the name and value of the variable binding relationship

  • Built-in namespace: python own name, in the python interpreter starts to produce, store some python built-in name
  • Global name space: When a file is stored at the file level defined name
  • The local name space: in the course of implementation of the document, if the function is called, it will produce the name of the function space used to store the defined within the function name will take effect when the function is called, after the end of the call failure

Order namespaces

  • Load order: built-in namespace -> global namespace -> local name space
  • Search order: Local namespace -> global namespace -> built-in namespace

Scope: The scope of the action generated

  • Global scope: global survival, globally effective
  • The local scope: local survival, local effective
x = 1

def f2():
    x = 2  # x=2只能在f2中使用
    print(x)
    
print(x)  # 1
f2()  # 2

If you really want to change the global variables or local variables

Global change name
x = 1
def f1():
    global x
    x = 2
f1()
print(x)  # 2
Change the local name
x = 0
def f1():
    x = 1
    def f2():
        x = 2
        def f3():
            nonlocal x #改的是函数正上方的值
            x = 3
            print('f3',x)  # f3 3
        f3()
        print('f2',x)  # f2 3
    f2()
    print('f1',x)  # f1 1
print(x)  # 0
f1()
Recommended reference types
lt = [1,2,3]  # 作用域关系仅适用不可变数据类型,不适用于可变数据类型
def f1():
    lt.append(4)
print(lt)  # [1, 2, 3]
f1()
print(lt)  # [1, 2, 3, 4]

Guess you like

Origin www.cnblogs.com/Gredae/p/11329301.html