Home > Backend Development > Python Tutorial > What is the Python delivery method? Speed ​​this article to understand Python parameter passing

What is the Python delivery method? Speed ​​this article to understand Python parameter passing

Tomorin
Release: 2018-08-14 14:22:25
Original
1726 people have browsed it

This is an article about Python parameter transfer method. It mainly introduces the Python parameter variable parameter definition and its Python parameter transfer method. Share it with everyone For reference. The specific analysis is as follows:

The so-called Python parameters

In python parameters, the type belongs to the object, and the variable has no type :

a=[1,2,3]
 a="PHPCN"
Copy after login

In the above code, [1,2,3] is List type, "PHPCN" is String type, and variable a has no type, she is just a reference to an object (a pointer), It can be a List type object or point to a String type object.

⊙mutable and immutable objects

In python, strings, tuples, and numbers are immutable objects, while list, dict, etc. are objects that can be modified.

·Immutable type: After the variable is assigned a=5, it is assigned a=10. Here, a new int value object 10 is actually generated, and then a is pointed to it, and 5 is discarded. , instead of changing the value of a, it is equivalent to newly generating a.

·Variable type: variable assignment la=[1,2,3,4] and then assignment la[2]=5 is to change the third element value of list la The change itself is not changed, only some of its internal values ​​are modified.

Python parameter passing method:

·Immutable type: C-like value transfer, such as integers and characters String, tuple. For example, fun(a) only transfers the value of a and does not affect the a object itself. For example, modifying the value of a inside fun(a) only modifies another copied object and does not affect a itself.

·Variable type: similar to c reference transfer, such as list, dictionary. For example, fun (la) is actually passing la. After modification, la outside fun will also be affected. It should be said that passing immutable objects and passing mutable objects.

⊙python Pass immutable object instance

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 def ChangeInt( a ):    
 a = 10
 b = 2ChangeInt(b)
 print b # 结果是 2
Copy after login
There is int object 2 in the instance, and the variable pointing to it is b, which is passed to ChangeInt function, the variable b is copied by value. Both a and b point to the same Int object. When a=10, a new int value object 10 is generated and a points to it.

⊙Passing a variable object instance

 #!/usr/bin/python
 # -*- coding: UTF-8 -*-
 # 可写函数说明def changeme( mylist ):   
"修改传入的列表"
   mylist.append([1,2,3,4]);   
 print "函数内取值: ", mylist
   return
 # 调用changeme函数
 mylist = [10,20,30];
 changeme( mylist );
 print "函数外取值: ", mylist
Copy after login
The object passed into the function and the object to add new content at the end are the same Quote, so the output result is as follows:

函数内取值:  [10, 20, 30, [1, 2, 3, 4]]
函数外取值:  [10, 20, 30, [1, 2, 3, 4]]
Copy after login

After-class extension:

Read through Python custom functions and Python function return values ​​in one article, with detailed examples

The above is the detailed content of What is the Python delivery method? Speed ​​this article to understand Python parameter passing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template