Fluent Python and Cookbook learning Notes (eight)

Source: Internet
Author: User

1. The default parameters of the function must be immutable

If the default parameter of a function is a mutable object, then the default parameter is modified outside the function and affects the function itself.

def spam (A, b=None):  # B to be an immutable parameter, you cannot use a variable parameter such as an empty list []       ... if  is None:          ... = []...

2. Anonymous functions

1. You can use anonymous functions when you can't think of a function name or want a short operation

Lambda x, y:x + y>>> sum (2, 3)def sum (x, y):   #  the anonymous function above, Equivalent to this function     ... Print (x + y) ... >>> sum (3, 4)7

2. Binding variables in anonymous functions

Lambda y:x + yLambda y:x +y>>> A (5)   #  found not expected 15 because X was changed 25>>> B (5)Lambda y, x=x:x + y  #  bind x,x as a local variable when defined, not affected by x change Lambda y, x=x:x + y>>> A (5)30>>> B (5)20

Fluent Python and Cookbook learning Notes (eight)

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.