Python Foundation 10 (function two)

Source: Internet
Author: User

The difference between a function parameter and an argument

Formal parameter names are formal parameters, and variable names in parentheses after the function name are used as formal parameters when defining a function with the DEF keyword.

The arguments are all called actual parameters, and the values provided when the function is called or variables are called as actual arguments.

def Add (b):   # A and B here are formal parameters    return A +b>>> add       #  1 and 2 Here are arguments 3>>> x=2            # the x and y here are arguments >>> y=3>>> Add (x, y)5  

Ii. transfer and change of parameters

In Python everything is an object, and a variable holds a reference to the object.

Example:

The print ID (5)   #  ID (object) function is an address that returns an object whose lifetime is in memory, and the parameter type of the ID function is an object print ID ('python'= 2print  ID (x)print ID (2  ' Hello ' Print ID (y) print ID ('Hello')

The result of the code operation:

As you can see from the running result, the value of ID (x) and ID (2) is the same, and the value of ID (y) and ID (' Hello ') are the same.

In Python everything is object, like 2, ' Hello ' is an object, but 5 is an integer object, and ' Hello ' is a string object. Above the x=2, the actual process in Python is this: first apply for a memory allocation to an integer object to store an integer value of 2, and then let the variable x point to the object, is actually pointing to this memory (here is a bit similar to the C language pointer). As with the result of ID (2) and ID (x), the description ID function returns the address of the object to which the variable is directed when it is acting on the variable. Because the variable is also an object, you can consider x as a reference to object 2 here.

Let's look at an example:

x=2print  ID (x) y=2print  ID (y) s='hello'print  ID (s) t=sprint ID (t)

Code Run Result:

From the running result you can see that the result of ID (x) and ID (y) is the same, and the result of ID (s) and ID (t) is the same. This means that x and Y point to the same object, and T and S are also pointing to the same object.

x=2 This sentence makes the variable x point to the Int type of object 2, and y=2 this sentence does not re-allocate space for 2, but let y point directly to the existing int type Object 2.

This is very well understood, because in itself just want to assign a value of 2, and in memory already exists such an int type Object 2, so directly to let y point to the existing object.

This will not only achieve the goal, but also save memory space. T=s This variable is assigned to each other, is also equivalent to let T point to the existing string type of the object ' hello ' (this principle and C language pointer in the reciprocal assignment of a bit similar).

Let's look at an example below:

x=2print ID (2)print ID  (x) x=3print ID (3)print  ID (x) L=[1,2,3]m=lprint  ID (l) print ID (M)Print ID (l[2]) l[0]=2print  ID (L)print M

Code Run Result:

Two times the value of the ID (x) is different, this may be a bit difficult to understand. Note that in Python, objects of a single element are not allowed to change , such as Integer data, strings, floating-point numbers, and so on.

X=3 the execution of this sentence is not to get the address of the object originally pointed to X, and then change the value in memory to 3, but the new request for a memory to store object 3, and then x to point to Object 3, so the value of two times ID (x) is different.

However, after changing the value of one of the child elements in L, the value of ID (l) has not changed. in Python, objects of complex elements are allowed to change , such as lists, dictionaries, tuples, and so on.

A variable in Python stores a reference to an object, and for a list, its ID () value returns the storage address of the first child element of the list l[0].

As in the example above, l=[1,2,3], where L has three child elements l[0],l[1],l[2],l[0], l[1], l[2] respectively, point to Object 1, 2, 3,id (L) value and object 3 are the same storage address.

Because L and M point to the same object, after changing the value of the L neutron element, m also changes, but the ID (l) value does not change, because the l[0]=2 just let l[0] point to Object 2 again, and L[0] itself has not changed the storage address, so ID (L) The value is not changed (the value of ID (L) is actually equal to the storage address of l[0] itself).

Take a look at the function parameter transfer and change the problem

parameter passing in Python takes a value pass , which is a bit like the C language.

1 defmodify1 (m,k):2m = 23K = [4,5,6]4     return 5     6 defmodify2 (m,k):7m = 28K[0] =09     returnTen      Onen = 100 AL = [N/A] - modify1 (n,l) - PrintN the PrintL - modify2 (n,l) - PrintN - PrintL

Code Run Result:

As can be seen from the results, N and l have not changed since the execution of Modify1 (), and N has not changed since the execution of Modify2 (), and L has changed. Because parameter passing in Python takes a value-passing method, the ID () values of N and L are obtained first, and then the parameters m and k are allocated space, and the M and K points to object 100 and object [three-way], respectively, when the function modify1 is executed.

m=2 this sentence to re-point m to object 2, and k=[4,5,6] This sentence let K again point to the object [4,5,6]. This change does not affect the arguments N and L, so there is no change in N and L after the execution of Modify1, and when the function modify2 is executed,

Similarly, let M and K point to Object 2 and object [three-way], but k[0]=0 let K[0] again point to the object 0 (note that K and L point to the same memory), so any changes to the memory of K point will also affect the L, so after the execution of Modify2, L changed.

Iii. Scope of variables

Local Variables when you declare variables within a function definition, they are not related to other variables with the same name outside the function, that is, the variable name is local to the function. This is called the scope of the variable. All variables are scoped to their defined blocks, starting with the point where their names are defined.
def func (x):     Print ' x is ' , x     = 2    print'Changed local xto'= +func (x)  Print'x is still', X

Output:

Principle: In the function, when we first use x the value , Python uses the value of the formal parameter declared by the function.

Next, we assign the value 2 to x . xis a local variable of the function. So, when we change the value within the function, the definition in the x main block x is not affected.

In the last print statement, we proved that the values in the main block are x really not affected.

All Variables It is defined outside the function, the scope is the entire file, the global variable can be applied directly inside the function, but if you want to change the global variables inside the function, you must use the Global keyword is declared.

def func ():     Global x     Print ' x is ' , x     = 2    print'Changed local xto'=-func ()  Print'Value of xis', X

Output:

Principle: global statements are used to declare x global-so when we assign values within a function x , the change is reflected in the values we use in the main block x .

Python Foundation 10 (function two)

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.