Python function parameter passing

Source: Internet
Author: User

parameter passing:

The argument is passed by automatically assigning the object to the local variable, and the object passed as a parameter is never automatically copied

The assignment of the parameter name inside the function does not affect the caller

Changing the value of a variable object parameter to a function may have an effect on the caller (in essence: immutable objects, such as String simple type tuples) are passed by ' value ', and mutable objects such as dictionary lists are passed through ' pointers '.

>>> def Change (A, B):
a=2
Print ID (b)
Print B
b[0]= ' spam '
Print B

>>> x=1
>>> l=[1,2]
>>> Change (x,l)
27900424
[1, 2]
[' Spam ', 2]
>>> b


Traceback (most recent):
File "<pyshell#87>", line 1, in <module>
B
Nameerror:name ' B ' is not defined
>>> ID (L)
27900424
>>>
b is a local variable name, but it is passed to a Mutable object, because the second assignment is the change of the object that occurred in the same place, the result of assigning the value of b[0 in the function will affect the value of L after the function returns, in fact we have not modified B, we have modified the part of the object that B is currently referencing. And this change will affect the caller

Python function parameter passing

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.