Python default parameters

Source: Internet
Author: User

I just read a question from a person in the group two days ago and record it. _ Author _ = 'chenyu 'def f (I, L = []): L. append (I) return L def f2 (I, L = None): if L is None: L = [] L. append (I) return L print f (1) print f (2) print f (3) print f2 (1) print f2 (2) print f2 (3) output: [1] [1, 2] [1, 2, 3] [1] [2] [3] What is output according to the expected result f () is [1], [2], [3], but the last result is retained. This is because python supports the default value of the function parameter as a variable list. The original default value is modified in the function body, and python keeps the modified value, it is used as the default parameter value for the next function call.

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.