Python Advanced Usage Summary

Source: Internet
Author: User
Tags iterable wrapper

Python is great, it has a lot of high-level usage worth pondering and learning to use. Based on daily use, this article summarizes a set of advanced features of Python, including: List derivation, iterators and generators, adorners.

Listing derivation (list comprehensions)

Scenario 1: Merge the elements of a three-dimensional list with all the one-dimensional data in a to form a new two-dimensional list.

The simplest method: Create a new list, traverse the original three-dimensional list, and determine if the one-dimensional data is a, and if it is a, append the element to the new list.
Cons: Code is too cumbersome, and for Python, execution slows down a lot.
For Scenario 1, we should first think of a list parsing to solve the process, a line of code to solve:

lista = [item for item in array if item[0] == 'a']

So, what is a list of analytic expressions?
Official Explanation: List parsing is a very simple and powerful build of Python built-in that can be used to create lists.
How strong is the concrete embodiment?
As you can see, the use of list parsing is much shorter, in addition, because it is a python built-in usage, the bottom level is implemented in C, which runs faster than writing Python code.

Scenario 2: For a list, you have to traverse the index and iterate through the elements.

Here you can use the Python built-in function enumerate to get the index better in the loop.

array = ['I', 'love', 'Python']for i, element in enumerate(array):    array[i] = '%d: %s' % (i, seq[i])

You can refactor it using a list deduction:

def getitem(index, element):    return '%d: %s' % (index, element)array = ['I', 'love', 'Python']arrayIndex = [getitem(index, element) for index, element in enumerate(array)]

It is said that this writing is more pythonic.

Summary: If you want to do some processing on an existing iterative object, and then generate a new list, using the list derivation will be the most convenient way.

Iterators and Generator iterators (Iterator)

The iterations here can refer to a for loop, in Python, for loops like list,dict and files, but they are not iterators, they belong to an iterative object.
What can iterate objects
The simplest explanation: You can use the for...in ... The object that the statement loops is an iterative object (iterable) that can be judged using the Isinstance () method.

from collections import Iterable type = isinstance('python', Iterable)print type

What is an iterator
Iterators refer to objects that can be recalled using the next () method, which can be converted to iterators using the ITER () method on an iterative object.

temp = iter([1, 2, 3])print type(temp)print next(temp)

At this point, temp is an iterator. So, an iterator is based on two methods:

    • Next: Go back to the next item
    • ITER returns the iterator itself

An object that can be understood to be called by the next () function and continually returns the next value is an iterator that defines both methods when defining an adorner.

Benefits of Iterators

When building an iterator, instead of loading all the elements one at a time, the element is returned when the next method is called, so there is no need to consider memory issues.
Iterator Scenarios

So, what is the specific scenario in which an iterator can be used?

    • The data size of the series is huge.
    • The sequence is regular, but cannot be described using a list deduction.
Generator

A generator is an advanced iterator that makes the code needed to return a function of a series of elements more simple and efficient (unlike creating iterator code).
Generator functions
The generator function can pause a function and return an intermediate result based on the yield instruction. When you need a function that will return a sequence or execute in a loop, you can use the generator, because when these elements are passed to another function for subsequent processing, returning an element at a time can effectively improve overall performance.
A common scenario is a stream data buffer that uses a generator.

Builder expression
A built-in expression is a convenient way to implement a generator, replacing the square brackets of a list deduction with parentheses.
and list derivation differences: List generation can create a table directly, but the generator expression is a side loop edge calculation, so that the list of elements can be calculated in the loop process one by one, do not need to create a complete list, thus saving a lot of space.

g = (x * x for x in range(10))

Summary: The generator is an advanced iterator. The advantage of the generator is that it delays the calculation and returns one result at a time, which is ideal for calculations of large data volumes. However, one thing you must note with the builder is that the generator can traverse only once.

Lambda expression (anonymous function)

The lambda expression is designed purely to write simple functions, and acts as a function sketch, allowing simple functions to be expressed in a more concise way.
The difference between Lambda and def
Lambda expressions can eliminate the process of defining functions, making the code more concise, suitable for simple functions, and writing functions that handle larger business needs to use DEF definitions.
Lambda expressions are often paired with map (), reduce (), and the filter () function

    • Map (): The map function accepts two parameters, one is a function and the other is a sequence in which the function can receive one or more parameters. Map passes the incoming function to each element in the sequence sequentially, returning the result as a new list.
      #将一个列表中的数字转换为字符串 map(str, [1,2,3,4,5,6])

    • Reduce (): The function receives two parameters, one is a function, the other is a sequence, but the function must receive two parameters reduce the result to continue and the next element of the sequence to do the cumulative calculation, the effect is reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2 ), x3), X4).

    • Filter (): This function is used for filtering, passing the passed function to each element in turn, and then deciding whether to leave or discard the element, depending on whether the function's return value is true or false.

Decorative Device

The adorner essence is a Python function that lets other functions add extra functionality without any code changes. With adorners, we can draw out a lot of similar code that is not related to the function itself and continue to reuse it. Often used for scenes with aspect requirements: including insert logs, performance tests, transaction processing, caching, and permission validation.
So why introduce adorners?

Scenario: Calculates the execution time of a function.

One way to do this is to define a function that specifically calculates the run time of the function, and then processes the actual business code after the run-time calculation is complete, with the following code:

import time def get_time(func):    startTime = time.time()    func()    endTime = time.time()    processTime = (endTime - startTime) * 1000    print "The function timing is %f ms" %processTimedef myfunc():    print "start func"    time.sleep(0.8)    print "end func"get_time(myfunc)myfunc()

However, the logic of this code destroys the original code logic, that is, calls to all Func functions need to be implemented using Get_time (func).
So, is there a better way to show it? Of course, that's the decorator.
Writing a simple adorner
In conjunction with the above examples, write adorners:

def get_time(func):    def wrapper():        startTime = time.time()        func()        endTime = time.time()        processTime = (endTime - startTime) * 1000        print "The function timing is %f ms" %processTime    return wrapper    print "myfunc is:", myfunc.__name__myfunc = get_time(myfunc)print "myfunc is: ", myfunc.__name__myfunc()

In this way, a simple complete adorner is implemented, and you can see that the adorner does not affect the execution logic and invocation of the function.
In Python, you can use the "@" syntax sugar to refine the adorner's code, changing the previous example to:

@ get_timedef myfunc():    print "start func"    time.sleep(0.8)    print "end func"print "myfunc is: ", myfunc.__name__myfunc()

* * Adorner's call order * *
Adorners can be used overlay, if multiple adorners decorate a function at the same time, then the call order of the adorner and the @ Syntax Sugar declaration order opposite, that is:

@decorator1@decorator2def func():    pass

is equivalent to:

Func = Decorator1 (Decorator2 (func ()))

Decorated function with parameters
In the above example, MyFunc () is not a parameter, and if the parameter is added, how can the adorner be written?

#被装饰的函数带参数def get_time3(func):    def wrapper(*args, **kwargs):        startTime = time.time()        func(*args, **kwargs)        endTime = time.time()        processTime = (endTime - startTime) * 1000        print "The function timing is %f ms" %processTime    return wrapper@ get_time3def myfunc2(a):    print "start func"    print a    time.sleep(0.8)    print "end func"a = "test"myfunc2(a)

Adorner with parameters
The adorner has a lot of flexibility, and it natively supports parameters, such as in the above example, the only parameter @get_time adorner is the function that executes the business, of course, you can also add parameters to the adorner and logically judge it.

Built-in adorners

In Python, common class decorators include: @staticmathod, @classmethod, and @property

    • @staticmethod: A static method of a class, distinguished from a member method, is that there is no self parameter and can be called if the class is not instantiated.
    • @classmethod: The difference from a member method is that the first parameter received is not self, but CLS (the concrete type of the current class)
    • @property: Represents information that can be accessed directly from the class instance directly.

Above, is the advanced usage of Python in this collation, this article will be updated continuously.

Python Advanced Usage Summary

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.