How are parameters passed in the Python function?

Source: Internet
Author: User

Everything in Python is an object, and the argument in the function passes the object's reference.

1 Change the object that the variable points to in the function, that is, point to a different object.

When you modify a passed-in variable in a function to point to another object, the object of the argument does not change.

>>>def Fun (num,l,d): Num=123;. .. l=[1,2,3] ... d={'a':123} ... print ("Inside:","num=%f,l=%s,d=%s"%(num,l,d)) ...>>> num=1>>> l=[1,1,1]>>> d={' Nice':111}>>> Print ("Before:","num=%f,l=%s,d=%s"%(num,l,d)) Before:num=1.000000, l=[1,1,1],d={' Nice':111}>>>Fun (num,l,d) Inside:num=123.000000, l=[1,2,3],d={'a':123}>>> Print ("After :","num=%f,l=%s,d=%s"%(num,l,d)) After:num=1.000000, l=[1,1,1],d={' Nice':111}

It is necessary to note that the num,l,d inside the function and the num,l,d in the command line are different variables, except for the same name. Inside the function is the formal parameter, which is the argument in the command line.

2 Modify the contents of the passed in object, that is, not to let the parameters point to different objects, but by reference to modify the object content. Of course, this object must be mutable.

>>>def fun2 (num1,l1,d1): Num1=123. .. l1[0]=123. .. d1['a']=123... print ("Inside:","num1=%f,l1=%s,d1=%s"%(NUM1,L1,D1)) ...>>> num=111>>> l=[1,1,1]>>> d={'a':111,'b':0}>>> Print ("Before:","num=%f,l=%s,d=%s"%(num,l,d)) Before:num=111.000000, l=[1,1,1],d={'a':111,'b':0}>>>fun2 (num,l,d) inside:num1=123.000000, l1=[123,1,1],d1={'a':123,'b':0}>>> Print ("After :","num=%f,l=%s,d=%s"%(num,l,d)) After:num=111.000000, l=[123,1,1],d={'a':123,'b':0}

Summary: All objects in Python, a reference to the object is passed in the function, and when the parameter points to a different object, the argument does not change; When a parameter modifies the contents of an object by passing a reference, the actual participant changes because the formal parameter and the argument point to the same object. This is not only the transfer of function parameters, but also inside the function. When a variable assigns a value to another variable, the reference to the object, the reference to the object, feels equivalent to the address of the variable in the C language, but in Python, the address variable is generic and can point to any type, and when you change the address, it's quite a different object from Python. The address of the original variable does not change, so the content of the address that the original variable points to does not change. When you change the contents of the address through this address, the contents of the address that the original variable points to will also change.

How are parameters passed in the Python function?

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.