Reference for passing Python Parameters

Source: Internet
Author: User
For Python values. I encountered a problem today, abstracted as the test I code below
   1: #!/usr/bin/python2.6                                                                                                                  

   2:  

   3: def FuncTest1(str_list):

   4:   path = '/home/test/file'

   5:   str_list = path.split('/')

   6:  

   7: def FuncTest2(str_list):

   8:   path = '/home/test/file'

   9:   str_list.extend(path.split('/'))

  10:  

  11:  

  12: str_list = []

  13: FuncTest1(str_list)

  14: print 'FuncTest1: %s' % str_list

  15: str_list = []

  16: FuncTest2(str_list)

  17: print 'FuncTest2:%s' % str_list

 

That is, whether to pass the value or reference the test parameter.

The test result is:

 

In this analysis, the python parameters are passed with values. For a simple type, it refers to the reference (or pointer) of an object that is passed for a complex type or object ).

Therefore, for the above situation,

In functest1, the passed str_list points to a new result. It can be considered that the point of the temporary object is modified in the function. The external function remains unchanged.

In functest2, a method call is executed for str_list, and its modification is valid for incoming requests.

This is similar to Java.

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.