Advanced topics in the 19th chapter of the Python Learning manual 4th

Source: Internet
Author: User

" " Date: September 5-September 30 Requirements: 1. Summary of the book contents, finishing in the blog Park notes upload 2. After all the exercises in the course Note: "#" After the addition of the notes (42 pages per day, you can guarantee to read this book at the end of the month) "Key Notes" "chapter Exercises"-Heading 1, level two headings- Heading 2, Notes outline title, exercise title-Bold, 16px"
Key Notes

I. Function design concept

    • Coupling: Use parameters for input and return statement 1 for output
    • Coupling: Use global variables only when it's really necessary
    • Coupling: Do not change the parameters of a mutable type unless the caller wishes to do so
    • Aggregation: Each function should have a single, unified goal
    • Size: Each function should be relatively small
    • Coupling: Avoid changing variables directly in another module file

(Note: coupling is also called inter-block connection.) A measure of the degree of connection between modules in a software system structure. The tighter the connection between the modules, the stronger the coupling, the less the independence of the module, and the coupling between the modules depends on the complexity of the interface between the modules, the mode of invocation and the information transmitted. )

Two. Recursive functions

Recursion can require traversing the structure of any shape.

  

def Sumtree (L):     = 0    for in  L:        if not isinstance (x,list):            + =        xElse:            + = sumtree (    x)return  = [1,[1,[2,3],4],5]print(Sumtree (L))

Note:

isinstance (object, ClassInfo)Returns true if the argument object is an instance of ClassInfo, or if object is an instance of the subclass of the ClassInfo class. If object is not a given type, the returned result is always false.

Three. Anonymous function: Lambda

The lambda expression creates a function that can then be called, but it returns a function instead of assigning the function to a variable name.

    • Lambda is an expression, not a statement
    • The body of a lambda is a single expression, not a block of code

In addition to these, Def and Lambda can do the same kind of work.

Four. Why use lambda

Lambda acts as a function sketch, allowing the definition of a function to be embedded within the code used.

Five. Mapping functions in a sequence: map

  The map function applies the passed-in function to each element in a sequence object, and returns a list containing the results of all function calls.

  

>>> L = [x-ray]defreturn x + 10>>> list (map (add,l)) [11, 12, 13]

Because map expects to pass in a function, it happens to be one of the places that lambda usually appears:

>>> list (map (Lambda x:x+10, L)) [11, 12, 13]
>>> list (map (pow,[1,2,3],[1,2,3)) [1, 4, 27]

Six. Functional programming tools: filter and reduce

The map function is the simplest built-in function for functional programming: Functional programming means a tool that applies some function to a sequence .

>>> List (filter ((lambda x:x > 0), Range ( -5,5))#return great more zero[1, 2, 3, 4]

  Reduce

 from Import reduce>>> reduce (lambda x,y:x+y), [1,2,3,4,5])15

In addition, the built-in reduce allows an optional third parameter to be placed before the items of the sequence, thus acting as a default result when the sequence is empty.

Import operator,functools>>> functools.reduce (operator.add,[2,12,5])19

This chapter exercises:

1. What is the relationship between lambda expressions and DEF statements?

A: Both lambda and def create function objects for later invocation. However, Lambda is an expression that can be embedded in a function definition where the DEF syntax does not appear.

The use of lambda can always be replaced with Def, and the function is referenced by the variable name.

Syntactically, lambda only allows a single return value expression because it does not support code blocks and does not apply to larger functions.

2. What is the point of using lambda?

A: Lambda allows the "inline" small unit to execute code, defer its execution, and provide state for it in the form of default parameters and enclosing scope variables. Lambda is not required and can always be replaced by def. It usually appears in callback-based programs such as the GUI, and they are closely related to map and filter, a function tool that expects a handler function.

3. Compare and contrast map, filter, and reduce

A: Map passes each item to the function and collects the results

Filter collects items that return a true value for those functions

Reduce calculates a single value by applying a function to an accumulator and subsequent items.

4. What are function annotations and how do I use them?

A: Python provides a special syntax for declaring annotations, but it doesn't do anything by itself, annotations are completely optional, and when they occur, they are simply attached to the function object __annotations__ property for other users to use.

5. What are recursive functions and how do I use them?

A: Recursive function calls themselves can be made directly or indirectly, thus implementing loops. They can be used to traverse the structure of arbitrary shapes, but they can also be used for general iterations.

6. What are the general design rules for writing functions?

A: Functions should usually be small, self-contained, have a single, uniform purpose, and communicate with other parts such as input parameters and return values.

Advanced topics in the 19th chapter of the Python Learning manual 4th

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.