PYTHON_TIPS[5]-variable data type as the initial taxiing parameter

Source: Internet
Author: User

Variable data type as initial taxiing parameter/Mutable Parameter as Init Formal-para

Because in Python, there is no static parameter like C, so when a function needs a parameter that is initialized only once, it is often inconvenient to initialize it outside of the function.

In Python, however, you can use a mutable parameter as the parameter default for a function to do this.

Full code

1 #N is mutable2 defFoo_1 (x, n=[]):3     Print(ID (n))4n + =[x]5     Print(ID (n))6     Print(n)7 8Foo_1 (2)9Foo_1 (3)TenFoo_1 (5) One  A Print(20*'-') - #N is immutable - defFoo_2 (x, n=()): the     Print(ID (n)) -n + =(x,) -     Print(ID (n)) -     Print(n) +  -Foo_2 (2) +Foo_2 (3) AFoo_2 (5)
View Code

Segment resolution

First, a function is defined, where the default value of the parameter n is a mutable type of list, in the function, the ID of n is first viewed, then the n is manipulated, the passed parameter value is added, the ID of n is viewed again and the value of N.

  1  #   n is mutable   2  def  foo_1 (x, N=[]):   3  print   (ID (n))   4  n += [x]   5  print   (ID (n))   6  print  Span style= "COLOR: #000000" > (n)   7   8  Foo_1 (2)   9  foo_1 (3 )  10  foo_1 (5) 

Test with 3 numbers, output results

55021192  55021192  [2]  55021192  55021192  [2, 3]   55021192  55021192  [2, 3, 5]

As you can see from the output, the 3-time function Call's n is the same ID, and the value of n is not reinitialized every time the function enters, that is, the parameter n is redirected each time it is called, pointing to the address of the initialized value. However, because n points to a mutable list, when n points to a value that has been changed, the value of that address is the value that is changed after it is re-pointed to that address.

Next, use immutable tuples for testing, similar to the above.

  1  #   n is immutable   2  def  foo_2 (x, N= ()):   3  print   (ID (n))   4  n += (x,)   5  print   (ID (n))   6  print  Span style= "COLOR: #000000" > (n)   7   8  Foo_2 (2)   9  foo_2 (3 )  10  foo_2 (5) 

< Span style= "Font-family:times New Roman, Times" > output  

4456520  52583336  (2,)  4456520  16619168  (3,)   4456520  52583336  (5,)  

As you can see from the final output, unlike the list, each output tuple is the element that was passed in. By looking at the ID of a tuple, the tuple is immutable, so each time an add operation is made, the tuple is actually re-directed, and the original empty tuple is not modified, so when the function is re-entered, the parameter n again points to the ID of the empty tuple, and the value is reinitialized.

PYTHON_TIPS[5]-variable data type as the initial taxiing parameter

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.