Python full stack development: Python function advanced

Source: Internet
Author: User
Tags closure

Python function nested Function object namespace and scope closure function adorner one, function nesting

Nested Definitions of functions

def func1 ():     Print ('fromfunc1')     def # func2= memory address        Print ('fromfunc2')     Print (FUNC2) func1 ()

Nested calls to functions

#Compare the size of a two numberdefmax2 (x, y):ifX >y:returnxElse:        returny#Compare the size of a three numberdefmax3 (x, Y, z): Res1=max2 (x, y) res2=max2 (res1,z)returnRes2Print(Max3 (11,199,2))

# Results
# 199
Second, the Function object

function is the first class of objects in Python

1. Can be referenced

X=1y=xdef  Bar ():    print('frombar'  F=bar   #  The value that bar represents when assigned as an object is the memory address F ()

2. Parameters can be passed in (the nature of the function name, memory address of functions)

F=func

3. The return value of the function can be

def Bar (x):     Print (x)    x () bar (func)

4. Elements of the container type can be

def # X=func    return # return func Res # Res=func # Print (res)res ()    
Namespaces and scope namespaces: space to hold names and binding relationships

Namespaces fall into three categories

Built-in namespaces: holds the Python interpreter's own name, takes effect when the interpreter starts, and fails when the interpreter is closed

Global namespace: A file-level name that takes effect when the file is executed and is invalidated when the file ends or is deleted during file execution

Local namespaces: The names defined in the stored functions (parameters of functions and names within functions are stored and local namespaces), #在函数调用时临时生效, function ends are invalidated

Load order and order of values between three namespaces:
Load order: Built-in (load on interpreter startup, load before program runs)--global (loaded from top to bottom)--local (loaded at call time)---> Built-in

Value: In local call: Local namespace---> global namespace---> Built-in namespaces

To look at the global scope:

Global----Built-in----local use:

the global cannot use local

The local can use the global

Scope : Is the range of action

1, namespaces and scopes are inseparable.

2, the scope is divided into two kinds

Global scope: The global namespace and the name of the built-in namespace are globally scoped and can be referenced anywhere in the entire file, effectively globally

Local scope: Local namespace, only valid locally

3, stand in the global view

When using a name, if it is global, use the global

If the global is not, use the built-in

4, why should have scope

For variables within a function do not affect the global

5.

Pass

Four, closed packet function

Closures: 1. Closed: Internal function
2. Package: Contains a reference to a variable in the scope of an external function

def Hei (): x=20 def  Inner ():x# if x is defined, he will use his own, it will not implement the closure print
#def  Hei (): x=20 def  Inner ():  " ' closure function "printreturn  inner () the common form of the closure function     

Method of judging closure function: __closure__

#the __closure__ of the output has a cell element: a closure functiondeffunc (): Name='Eva'    definner ():Print(name)Print(Inner.__closure__)    returnInnerf=func () f ()#the output __closure__ is none: Not a closure functionName ='Egon'defFunc2 ():definner ():Print(name)Print(Inner.__closure__)    returnInnerf2=Func2 () f2 ( )
View Code

Closed Packet Acquisition Network application

# From urllib.request import Urlopen # def index (URL): #      def inner ():#         return  urlopen (URL). Read ()#     return Inner#  u= ' Http://www.cnblogs.com/Eva-J/articles/7156261.html#_label1 '# get = Index (u) # print (Get ())
View CodeFive, decorative device

Pass

Python full stack development: Python function advanced

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.