Reveal the secrets of Python features

Source: Internet
Author: User

The Ruby language is very similar to Python, but the overall structure is still not the same. This language has very simple and clear syntax characteristics and is suitable for completing various high-level tasks, it can run in almost all operating systems, so it is favored by programmers.

This is a secure answer, but it cannot be clarified very accurately. Unfortunately, it is difficult for function programmers to have a consistent understanding of what FP is. It seems appropriate to use the story of "blindly touching people" to describe this situation. You can also safely use Python features and "command programming" to perform operations such as C, Pascal, C ++, Java, Perl, Awk, TCL, and most other languages, at least to a large extent.

From a personal perspective, I will roughly depict function programming as having at least the following features. A functional language makes these things simple and makes other things difficult or impossible: a function is the first class object ). That is, every operation that can be performed on "data" can be performed using the function itself, for example, passing a function to another function ).

  • Big secrets about Python programs
  • How to Use the Python module to parse the configuration file?
  • How to Write Python programs better and faster
  • Deep Analysis of Python function programming skills
  • What are the goals of designing Python?

Recursion is used as the main control structure. In some languages, there are no other "loop" structures. Focus on LISt processing, for example, name Lisp ). Lists are often used together with sublists recursively to replace loops. "Pure" function language can avoid side effects. This does not include the most common mode in the command language, that is, to specify the first, and then specify the other value to the same variable to track the state of the program.

FP does not encourage or prohibit statements at all. Instead, it uses expressions to evaluate values. In other words, the function is added with independent variables ). In pure cases, a program is an expression plus supported definitions ). FP is concerned with computing rather than how to calculate. Many FP use "Higher-Level" functions. In other words, they operate on some functions, and these functions operate on other functions ).

Function programming advocates believe that all these features lead to faster development of less code and fewer errors. Moreover, senior theoretical scientists in computer science, logic, and mathematics have discovered that the formal performance of function languages and programs is much easier than that of command languages and programs. Since Python 1.0, Python has seen most of the FP features listed above. But for most Python features, they are presented in a very mixed language.

Largely because of the OOP feature of Python, you can use the desired part and ignore the rest until you need it later ). Using Python 2.0, the list content adds some great "syntactic whitewashing ". Although the list content does not add any new capabilities, they make many old capabilities look much better.

The basic elements of FP in Python are map (), reduce (), filter (), and operator lambda. In Python 1.x, The apply () function is convenient for directly applying the return values of a function list to another function.

Python 2.0 provides improved syntax for this purpose. It may be surprising, but few of these functions and basic operators are enough to write any Python program. In particular, all flow control statements such as if, elif, else, assert, try, break t, finally, for, break, continue, while, and def can be processed in the function style only using FP functions and operators.

Although it is actually useful to eliminate all the flow control commands in the program, it may only be useful to join the "chaotic Python" competition and code that looks like Lisp, however, it is worthwhile to understand how FP uses functions and recursion to represent stream control.

Lambda Short Circuit in Python

 
 
  1.   # Normal statement-based flow control   
  2.           
  3.           
  4.           if  
  5.          <cond1>:   func1()   
  6.           
  7.           elif  
  8.          <cond2>: func2()   
  9.           
  10.           else  
  11.         :         func3()   
  12.    
  13.         # Equivalent "short circuit" expression   
  14. (<cond1>   
  15.           
  16.           and  
  17.          func1())   
  18.           
  19.           or  
  20.          (<cond2>   
  21.           
  22.           and  
  23.          func2())   
  24.           
  25.           or  
  26.          (func3())   
  27.    
  28.         # Example "short circuit" expression   
  29. >>> x = 3   
  30. >>>  

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.