Introduction to Python version Selection

Source: Internet
Author: User

This article describes the Python version selection, IDE selection, and encoding solutions in detail. It is a learning experience for beginners of Python, one way is that the main program uses a high-level language, such as C ++, to write it, and then embed a script interpreter to dynamically execute some script functions at runtime;

But just as we can use number 23 without binding it to any name, in other words, like a function independent variable ), we can use a function object created with lambda without binding it to any name. A function is just another value for performing some operations on it in Python.

The main operation we perform on the first type of objects is to pass them to the built-in FP functions map (), reduce (), and filter (). Each of these functions accepts the function object as its first independent variable. Map () executes the passed function for each corresponding item in the specified list and returns the result list.

Reduce () executes the passed function for each subsequent item and returns the internal accumulation of the final result. For example, reduce (lambda n, m: n * m, range )) it means "10 factorial". In other words, use the product of each forward multiplication of each item ).

  • Introduction to the Python Interpreter
  • How to Create a Python multi-threaded Environment
  • Analysis on Python Multithreading
  • How to establish a main Python thread?
  • Instructions on executing the Python Interpreter

Filter () uses the passed function to evaluate each item in the list, and then returns the list of checked items that pass the function test. We also often pass function objects to our own custom functions, but they are usually equivalent to the combination of the built-in functions above.

By combining these three built-in FP functions, You can execute a series of amazing "stream" operations without using statements, but only using expressions ). The function replacement loop in Python is as simple as the replacement condition block.

For can be directly converted to map (). For Conditional execution, we need to simplify the statement block into a single function call. We are gradually approaching the usual practice): while conversion still requires the while_block () function, it contains statements, not just expressions. But we need to further eliminate this function, for example, short-circuit the if/else in the template ).

In addition, because the cyclic subject cannot change any variable value according to the design, it is difficult to <cond> use it in a general test. For example, if while myvar = 7, it will be in while_block () ). One way to add more useful conditions is to let while_block () return a more interesting value, and then compare the returned value with the termination condition. It is necessary to take a look at the specific examples of these elimination statements:

 
 
  1. # imperative version of "echo()"   
  2. def echo_IMP():   
  3. while 1:   
  4. x = raw_input("IMP -- ")   
  5. if x == 'quit':   
  6. break   
  7. else   
  8. print x   
  9. echo_IMP()   
  10.  
  11. # utility function for "identity with side-effect"   
  12. def monadic_print(x):   
  13. print x   
  14. return x   
  15.  
  16. # FP version of "echo()"   
  17. echo_FP = lambda: monadic_print(raw_input("FP -- "))=='quit' or echo_FP()   
  18. echo_FP()  

What we have done is to try to represent a small program involving I/O, loops, and condition statements into a pure expression with recursion. In fact, if you need, function objects that can be passed to any other place ). We do still use the utility function monadic_print (), but this function is completely generic. It is a one-time cost to reuse it in every function expression we create later ).

Note that the results of any expression containing monadic_print (x) are the same, just as it contains only x. FP, especially Haskell) is "single body" for a function that does not execute any operation and has side effects in the process.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and coding Solutions
  5. Analysis of Python GIL and thread security

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.