[Python] Function traps

Source: Internet
Author: User

First, the local variable is static detection

The variable names that are assigned in the function are default to local variables (following the LEGB rule), they exist in the scope of the function, exist only when the function runs, the function ends, and the variables disappear.

Second, the default variable object

The default parameters of a mutable type save their state between function calls

Three, no Return statement function, returns none

1 defSaver (x=[]):2X.append (1)3     Print(x)4     5 Print(Saver ([2]))6 Print(Saver ())7 Print(Saver ())8 Print(Saver ())9 Ten #Output One[2, 1] A None -[1] - None the[1, 1] - None -[1, 1, 1] -None

[Python] Function traps

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.