Function object, nested namespace scope

I. Object Functions

 Functions are first-class objects: a value corresponding to the function name (function body) may be passed as a parameter
1. Functions that can be transferred (similar to variable)
 
= name ' Waller ' 
ures_name = name   # pass variable name 
Print (ures_name)   # >>> Waller

DEF FUNC ():
     Print ( ' name IS Waller ' )
 Print (FUNC)   # printing is the memory address of the function body of <function FUNC AT 0x000002EF3AA02EA0> 
Print (ID (FUNC))   # print function name in the memory of address 3226504015520 
F = FUNC   # FUNC memory address corresponding to the function assigned to F 
Print (F)   # printing is a function of the code memory address of the body <AT 0x000002EF3AA02EA0 function FUNC>

F () function call #, equivalent to FUNC ()
 

 
2. The function name can be passed as arguments to other functions
def func():
    print('from func')

def info(regs):
    print(regs)  # <function func at 0x000001A378C62EA0>
    regs()  # from func
    print('from info')  # from info
info(func)

 
3. The name of the function can be used as a function of the value is returned
def func():
    print('from func')
def info():
    print('from info')
    return func
info ()   # >>> info from the print function of the result of operation, and may receive a return value from the 
RES = info ()   # >>> # info from the return value of the function assigned to a variable RES 
Print (RES)   # >>> <function func at 0x000002C27E772EA0> # is equivalent to Print (FUNC) 
RES ()   # >>> from equivalent FUNC FUNC # ()

4. The container type name can be used as data elements in the
DEF func ():
     print ( ' from func ' )
 print (func ())   # () function, the results from the priority operation func func is, the return value is None print printed out. 9 
L = [1,2, func , FUNC ()]   # container type data is to FUNC () function returns the value into which, instead of the function is result 
Print (L)   # >>> [. 1, 2, <0x0000024143502EA0 function FUNC AT>, None]
 

II. Nested functions

Call other functions inside the function, you can simplify the complex logic
DEF my_max (X, Y):   # definition may compare function of the magnitude of two numbers 
    IF X> Y:
         return X
     return Y
 # Print (my_max (2,5)) >>> # print out the return value of the function is

def my_max1(a,b,c,d):
    RES1 = my_max (A, B)   # receiving two numbers compare the return value 
    RES2 = my_max (RES1, C)
    RES3 = my_max (RES2, D)
     return RES3   # receives the final return value 
Print (my_max1 (2,4,6,8))   # >>> #. 8 prints out the comparison of the number four return value

 

III. Namespace

Place to store the variable name, where the variable name is bound to address the relationship between the variables stored 
in order to access a value must be the namespace to find the variable name to access to variable values
 
Namespace Category: 

1. built-in namespace:
Python interpreter in advance defined name exists built-in namespace (len, max, Print, the Range ...)
2. global name space:
File-level code, programmers generally According to the naming convention variable names from the variable name
3. the local name space:
the variable name to create a function of the body belong to the local namespace

life cycle:
1. built-in namespace: python interpreter initiates the creation, python interpreter closed automatically destroyed
2. global namespace: right-run py file creation, py file has finished running automatically destroyed
3. The local name space: create a function call, the function is ended immediately destroy the (dynamically created, dynamic destruction)
 
Search order:
first determining a current position (*****):
The current position in the global: global >>> built
as a current location in the local; local >>> >>> built a global

function to find the name of the sequence on the definition phase have been identified, it will not be invoked as a function of position change and change (*****)

= name ' global namespace ' 
DEF FUNC ():
    name = ' local namespace ' 
    Print (name)
FUNC ()   # >>> local namespace 
Print (name)   # >>> global name space


name = ' global namespace ' 
DEF FUNC ():
     Print (name)
FUNC ()   # >>> global name space
 
# Variable names defined first and then call 
DEF index ():
     Print (the X-)
    X = 666 
index ()   # error
def index():
    x = 10
    print(x)

def index2():
    print(x)
index ()   # >>> the X- 
index2 ()   # error parallel function can not cross to find the variable name
= 111 X
 DEF Outer ():
     DEF Inner ():
         Print ( ' from Inner ' , X)   # function in order to find the name of the definition phase has been determined 
    return Inner
f=outer()
def func():
    X = 333 
    F ()   # does not change in position should call the function varies 
func () # >>> from inner 111

 

Scope

Global scope: effect globally 
local scope: local commencement

global: global local modification, a plurality of such modifications, separated by commas
nonlocal: partial modification topical, such as modifying the plurality, separated by commas
# Locally modify the global 
name = ' Waller ' 
Age = 20 is
 DEF FUNC ():
     Global Age, name   # statement 
    Age = 22 is 
    name = ' Wong ' 
    Print (name, Age)
func()  # wong 22
print(name)  # wong
print(age)  # 22
 
# Partial modification partial 
DEF FUNC ():
    x = 1
    def info():
        nonlocal x
        x = 2
    info()
    print(x)
func()  # >>> 2

 

 
 
 

Guess you like

Origin www.cnblogs.com/waller/p/11165799.html