How to compile Python code

Source: Internet
Author: User

Python calling is very practical in daily work. You only need Python functions. If you are not familiar with Python code, you must first learn the following, the following is a systematic analysis and research on the Python code.

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 code. But the most important feature-may also be the most useful feature in a specific situation.

It eliminates side effects or at least blocks 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 fairly common command code. 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.

  • Deep Analysis of Python function programming skills
  • What are the goals of designing Python?
  • Summary: Python version Overview
  • Zipfile In the Python Module
  • Notes for beginners of Python

The command to achieve this is as follows: This project is too small, so there is no possibility of error. 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. The following code is possible:

 
 
  1. bigmuls =   
  2.           
  3.           lambda 
  4.          xs,ys: filter(  
  5.           
  6.           lambda  
  7.          (x,y):x*y > 25, combine(xs,ys))  
  8. combine =   
  9.           
  10.           lambda 
  11.          xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs)))  
  12. dupelms =   
  13.           
  14.           lambda 
  15.          lst,n: reduce(  
  16.           
  17.           lambda  
  18.          s,t:s+t, map(  
  19.           
  20.           lambda  
  21.          l,nn=n: [l]*n, lst))  
  22.           
  23.           print  
  24.          bigmuls((1,2,3,4),(10,15,3,22))  

In this example, we bind the anonymous Python code with 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 code in the Command version, given that the utility function can be reused.

The real advantage of this function example is that there is absolutely no variable to change any of the values. There are no unexpected side effects in the code later, and the code earlier does not ). Obviously, it has no side effects and does not guarantee correct code, but even so, this is also an advantage. Note that Python code is different from many function languages)

Rebinding of names bigmuls, combine, and dupelms is not allowed. If combine () has other meanings at the beginning of a later part of the program, all efforts will be abandoned. You can gradually create a Singleton class to include this type of immutable binding, such as s. bigmuls). However, this column does not cover this content.

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.