python--function nesting and scoping

Source: Internet
Author: User

1. Ternary operation
The IF condition is the result of the if condition that the else condition is not established
For example:
A=20
b=10
C=a if a>b else b
Print (c)

2. Namespaces

    • Global namespaces: The space created to store "variable name-to-value relationships" is called the global namespace
    • Local namespaces: Temporary space created in the function's run is called a local namespace
    • Built-in namespaces: The built-in namespaces hold the Python interpreter's name for us: Input,print,str,list,tuple ... Wait a minute
    • Load order and order of values between three namespaces:
      Load order: Built-in (load before program runs)--global (loaded from top to bottom)--local (loaded when called)---> Built-in
      Value: In local call: Local namespace---> global namespace---> Built-in namespaces
      Global scope: Global----built-in----local
      Use:
        The global cannot use local
      The local can use the global

      3. Scope: is the range of action
      1. Namespaces and scopes are inseparable
      2. The scope is divided into two types:
      Global scope: Global namespaces and names of built-in namespaces are global
      Can be referenced anywhere in the entire file, valid globally
      Local scope: Local namespace, only valid locally
      3. Stand in the global view:
      When using a name: If global, use the global
      If the global is not, use the built-in
      4. Why should there be scope?
      For variables within a function do not affect the global

      5.globals method: View the name of the global scope print (Globals ())

      Locals method: View the name of the local scope print (locals ())

    • def func ():         =    locals    ()print(globals ()) func ()

      Standing in the global view, globals is locals
        Global keyword: cast to globals

    • # x=1 # def foo (): #      Global  x  #强制转换x为全局变量 #     x=10000000000#     Print (x) # foo () # print (x) # This method can be used as little as possible.
      Global

      Nonlocal let the variables in the intrinsic function take effect in the previous layer and the following layer functions (parent-level parent below)

    • #x=1#def f1 ():#x=2#def f2 ():## x=3#def f3 ():## Global x# modifies the overall#nonlocal x# Modify the local (when using nonlocal, modify the x=3 to x=100000000, when x=3 does not exist, modify x=2 for 100000000)## must be inside the function#x=10000000000#f3 ()#print (' Printing in F2 ', x)#F2 ()#print (' Printing in F1 ', x)#F1 ()## Print (x)
      nonlocal

      4. Nested Definitions of functions

    •  def   animal ():  def   Tiger ():  print  (
            
              " 
             nark  
             "  Span style= "COLOR: #000000" >)  
             print  (
              "
              eat   "  ) Tiger () animal ()  
            

      5. Scope

    •  x=1 def   Heihei (): x  ="  h   " def   inner (): x  ="  Span style= "COLOR: #800000" >il   " def   Inner2 ():  print   (x) Inner2 () inner () Heihei ()  

      6. The nature of the function name: the memory address of the function

    •  def   func ():  print  ("  func  "  )  print  (func) Span style= "COLOR: #008000" >#   

      7. Function names can be used as arguments to functions

    • def func ():     Print ('func') def Func2 (f):    F ()    print('func2') Func2 (func) Function names can be used as parameters
      Function names can be used as arguments

      Function name can be used as the return value of a function


    • return Description 1def func ():defFunc2 ():Print('Func2')    returnFUNC2F2=func () f2 ()#Func2=func ()#Func2 () 2.defF1 (x):Print(x)return '123'defF2 (): ret = F1 ('s')#F2 calls F1 function print (ret) f2 () 3.defFunc ():defFunc2 ():return 'a'    returnFunc2#function name as return value Func2=func () print (Func2 ())
      Call of function name and can be returned as value

      8. Closures:


    • 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=10 #如果x定义了, he uses his own, he can not achieve closures
      Print (x)

    • def Hei (): x=20 def  Inner ():  ' closure function ''print'  Common forms of C9>return inner () closure function     
      Common forms of closures:

      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 ( )
      The judging function is not a closure function

      Closed Packet Acquisition Network application

    • # From urllib.request import Urlopen # def index (URL): #      def inner ():#         return  urlopen (URL). Read ()#     return Inner#  u= ' http://www.xiaohua.com '#  get = Index (u)# print (Get ())
      Closures---Web site

python--function nesting and scoping

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.