Introduction to Python code

Source: Internet
Author: User

Before learning and studying Python code, we should first understand what Python is. The so-called Python is an object-oriented, literal translation computer programming language, it is also a powerful and complete general-purpose language with a history of more than 10 years.

After removing perfect and meaningful statements and replacing them with obscure and nested expressions, a natural question is: "Why ?!" All my descriptions of FP are implemented using Python. But the most important feature-probably the most useful feature in a specific situation-is that it eliminates side effects or at least has some restraining effect on some special fields, such as a single body ).

The vast majority of program errors-and issues that lead programmers to turn to debugging for solutions-occur because variables contain unexpected values during program execution. Function programs do not assign values to variables at all, thus avoiding this special problem.

Let's look at a piece of Python code that is quite common. It is used to print a list of digits with a multiplier value greater than 25. The numbers that make up each pair are selected from the other two lists. This operation is similar to the Operations actually performed by programmers in their program segments. The command to achieve this is as follows:

 
 
  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()  

This project is too small to cause any errors. However, our goal may be embedded in code that needs to implement many other purposes at the same time. Comments with "more stuff" are the places where side effects may cause errors.

In any of these places, the variables xs, ys, bigmuls, x, and y may obtain unexpected values in the hypothetical Section code. Besides, after executing this code, all variables may have values that may or may not be needed later.

Obviously, you can use function/instance encapsulation and related scope considerations to prevent such errors. Besides, you can always del the variables after they are executed. However, in practice, these error types are very common.

The Target Function Methods completely eliminate these side effects. Below is a possible SectionPython code:

 
 
  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()  

In this example, we bind an anonymous (lambda) function object to the name, but this is not necessary. We can just nest the definition. This is done for readability purposes, but it is also because combine () is a well-known utility function that generates a list Of all element pairs from two input lists ).

The subsequent dupelms () is mainly a method that helps combine () to take effect. Even if the function example is longer than the command example, the new code in bigmuls () may be less than the Python code in the Command version, given that the utility function can be reused.

  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

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: [email protected] 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.