"7" Python core programming chapter 11th-Functional and functional programming

Source: Internet
Author: User

1. * Function (and method) adorner
The main motivation behind the adorner stems from Python object-oriented programming. Adorners are adornments on top of function calls. These modifications are only applied when a function or method is declared. The adorner's syntax begins with @, followed by the name of the adorner function and optional parameters. The adorner is followed by the modified function, and the optional parameters of the adornment function. The adorner looks like this: @decorator (Dec_opt_args)def  func2bdecorated (Func_opt_args):: So what is an adorner? Now we know that adorners are actually functions. We also know that they accept function objects. But how do they deal with those functions? Generally, when you wrap a function, you end up calling it. The best part is that we can call it at the right time in a packaged environment. Before we execute the function, we can run some preparatory code, such as Post-morrem analysis, or we can do some cleanup after executing the code. So when you see an adorner function, it's possible to find some code in it that defines a function and embeds a call or at least some references to the target function somewhere within the definition. Essentially, these features introduce the concepts that Java Developers call AOP (Aspect oriented programming, aspect-oriented programming). You can reduce the complexity of your program by thinking about placing code in the adorner with common functionality. For example, you can use adorners to:?? Introduce logs?? Increase timing logic to detect performance?? Ability to add transactions to a function
2. Built-in function apply (), filter (), map (), reduce ()
The built-in function describes apply (func[, nkw][, KW])  using optional parameters to invoke func,nkw as a non-keyword parameter, the KW keyword parameter; the return value is the return value of the function call. Filter (func, seq) b calls a Boolean function, Func, to iterate through the elements in each SEQ, and returns a sequence of elements that enable Func to return a value of ture. Map (func, seq1[,seq2 ...]) functions func on each element of the given sequence (s) and provides the return value with a list, and if Func is None, Func behaves as an identity function, returning a list of n tuples containing the set of elements in each sequence. Reduce (func, seq[, Init]) applies the two-tuple function to the elements of the SEQ sequence, each time carrying a pair (previous results and the next sequence element), successively adding the existing result and the rain to the value on the resulting subsequent result, and finally reducing our sequence to a single return value If the initial value init is given, the first comparison will be init and the first sequence element rather than the first two elements of the sequence.

"7" Python core programming chapter 11th-Functional and functional programming

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.