Python advanced features

Source: Internet
Author: User


I always think that python is a magic language. The magic is that it can be either spring or winter. That is to say, it is easy for almost all people to get started after learning a little, but if you savor it, you will find that he still has many profound things. Just like a pretty girl, she will like it at a glance. In-depth interaction, you will find that she is proficient in piano, chess, and calligraphy, and has a very connotation, I believe you will be deeply infatuated with her at this moment.


This article aims to organize some advanced Python features and try to unveil the secrets of Python. The advanced features of python can be written into a book in depth, so this article is just a bit short. If you are interested, read the references below.

Function programming:

The most direct explanation of functional programming is to use functional thinking to think about problems. So what is functional thinking? In python, there are several functional programming topics:

1. Define anonymous functions.

Lambda can be used to define simple single-row anonymous functions. For example:

lambda_add = lambda x, y: x + y

This is exactly the same as the summation function defined by def. You can call it using lambda_add as the function name. However, lambda is provided to write anonymous functions that are occasionally, simple, and foreseeable to be modified.


2. Use the function as a parameter.

In python, functions can be passed as parameters. The reason is that the function is also an object. In python, functions are level-1 objects. You can use it like other objects without any special restrictions. A function is a common example of an object. It is a function pointer in C. It is used to pass a function to other functions that will use it.

>>> #A very simple function>>> def greeter():… print("Hello")…>>> #An implementation of a repeat function>>> def repeat(fn, times):… for i in range(times):… fn()…>>> repeat(greeter, 3)HelloHelloHello>>>

3. Closure

Definition: If a variable in the external scope (but not in the global scope) is referenced in an internal function, the internal function is considered as a closure ). In fact, the most accurate statement should be that the closure is an entity composed of a function and its reference environment. Let's look at the example below.

>>> a = 0>>> def get_a():… return a…>>> get_a()0>>> a = 3>>> get_a()3

In this example, A is a global variable, and a and get_a () form a closure. Therefore, when a is changed, it is directly reflected in the get_a () function.

Function: the function of closure is self-evident. It enhances modularity and abstraction. Besides, the decorator is implemented based on the closure.


4. decorator

The decorator is a well-known design model and is often used in scenarios with cut-plane requirements. It is more classic in terms of log insertion, performance testing, and transaction processing. The decorator is an excellent design for solving such problems. With the decorator, we can extract a large number of identical codes irrelevant to the function itself and continue to reuse them. In summary, the purpose of the decorator is to add additional functions to existing objects.
For example, if you want to calculate the execution time when calling a function, you can use the decorator, such as the following code

import time def timeit(func):    def wrapper():        start = time.clock()        func()        end =time.clock()        print 'used:', end - start    return wrapper @timeitdef foo():    print 'in foo()' foo()



5. built-in functions:

Such as map, reduce, filter, zip, and so on, but I think this can be done by list parsing, and it is more elegant.


Further reading and reference
Functional Programming

1. Timeout

2. Renew

 

Closure

1. http://linluxiang.iteye.com/blog/789946 Python closure research, the author from the perspective of assembly code to explain the characteristics and principles of closure

2. http://www.ibm.com/?works/cn/linux/l-cn-closure/analyzed the concepts, forms, and applications of closures in dynamic languages (Python, Ruby, and LUA ).

 

Decorator

1. Some key points of Aspect-Oriented Programming.

 

Others

1. The author of http://hyperpolyglot.org/scriptingcompares the explanatory languages (PHP, Perl, Python, and Ruby) from multiple perspectives!

2. Why? This article will certainly open your eyes to learn more about Python's amazing things.

 

Books:

Python source code analysis this book at the C code level, in-depth and meticulous analysis of Python implementation. The book not only contains an analysis of a large number of Python built-in objects, but also uses a large amount of space for analysis of Python virtual machines and advanced features of Python. After reading this book, I believe that you can understand the operating principles of general expressions, control structures, exception mechanisms, class mechanisms, multithreading mechanisms, dynamic module loading mechanisms, memory management mechanisms, and other core technologies in Python,


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.