Functions and scopes

Source: Internet
Author: User

1. function 1.1 General form
1 def f (): 2     Print ("OK") 3 f () 4 Print (f)

Function Effect:

(1) Avoid code duplication

(2) Strong extensibility

(3) Code consistency

It is important to note that the printed F is equivalent to the address of a variable, only () executes the function, the function has its own scope, and the function is pre-stored in memory, but execution is one line.

1.2 Formal parameters and arguments

1 def num (A, b): 2 print("Gaga is%s%s"%a,b)

The A/b in the above code is the formal parameter. The external data can be taken into the function by means of a formal parameter.

When you run the function, num (1 and 2) are the arguments. Arguments are entered in accordance with their own needs.

Note: Not only is the number input other such as: variables, functions, etc.

1.3 Return value
1 def f (N):                        2     return n3def  foo (a,b,func):4     return Func (a) +func (b)5print(foo (1,2,f))

The return value of the returned keyword is the return of the function. The function of the return value is to end the function and return a value.

As you can see from the code above:

(1) Return multiple values return will be made into a tuple

(2) When a function is an argument, it is important to note that parentheses are not

1.4 of four Ways

Functions >>> Execution

1.def f (b) >>> F (one by one) # must correspond to

2.def F (sex= "female") # has a default effect

3.def f (*args) >>> F (1,2,i,j) # must be unnamed or can be added as a tuple. But *[1,2,4] This is a tuple

4.def f (**kwargs) >>> F ("name" = "a", "LK" =23) # ibid., but all are key-value pairs.

The order of the four types is: 1234 Some of the details should be based on the test results. There is no ambiguity.

2. Scope
Int (a)
1A =12 deff ():3A =24b =95 Print(a)#26 defL ():7 #nonlocal b8b = 39 defp ():Ten nonlocal b OneB=4 A Print(b)#4 -A =4 - p () the Print(b)#4 - L () - Print(b)#9 -F ()

There are 3 functions nested here, a total of 4 layers, and the innermost layer as the fourth layer.

Scope of Thinking Search range is: LEGB

"Build in"    >>>   int
"Global" >>> a = 1
"Enclosing" >>> a = 2 b = 3
"Local" >>> b = 4 A = 4

Summarize:

(1) Look out from inside. In the innermost print (b) The interpreter will first look for B in this layer, and this layer will not find the outer b=3.
(2) The inner layer needs to use the outer variables. "If local and enclosing want to modify the internal layer in the enclosing to modify its previous layer (excluding global) with the nonlocal and can only modify the upper layer of the above is not affected" example is the b=9 in the comments
(3) "Variable cannot modify the previous layer without declaring it, even if the name of a variable is used in this layer (not the same variable)"

(4) If the fourth level does not declare nonenclosing B is printed B will cause the error is not to print after the first execution. This also shows that the interpreter stores the function first in memory.

Functions and scopes

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.