Python function definition default parameter must point to immutable object

Source: Internet
Author: User
Tags function definition

Why is the Python function definition default parameter must point to the immutable object?

The following is a personal understanding, if there is a wrong place, welcome to discuss

Cause analysis: When defined, the function allocates the memory space of the object to which the function and default parameter L point.

1. If L is pointing to a mutable object, and the variable object is updated in the function, the variable object is created when the function is called more than once, and the update is the function definition.

This mutable object is not destroyed as the function call ends because the function definition still exists.

     

2. If L is pointing to an immutable object, the function also updates L, at which point L points to the memory space of the newly created object, and the immutable object when the function is defined

has not changed. At the end of the function call, the memory space of the newly created object is destroyed. Because it was created when the function was called.

     

Python function definition default parameter must point to immutable object

Related Article

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.