Python Function Definition default parameter must point to immutable object, python Function

Source: Internet
Author: User

Python Function Definition default parameter must point to immutable object, python Function

Why do python Function Definition default parameters must point to immutable objects?

The following is my personal understanding. If you have any mistakes, please discuss them.

Cause Analysis: when defining a function, it allocates the memory space of the function and the default parameter L pointing to the object.

1. If L points to a mutable object and the variable object is updated in the function, the variable object created when the function definition is updated when the function is called multiple times,

This variable object will not be destroyed as the function call ends, because the function definition still exists.

2. If L points to an immutable object, L is also updated in the function, and L points to the memory space of the newly created object, and the immutable object during Function Definition

Not changed. When the function call ends, the memory space of the newly created object is destroyed. It is created only when the function is called.

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.