Python-Day4 Python basics advanced Builder/iterator/decorator/Json & amp; pickle data serialization, python-day4json

Source: Internet
Author: User
Tags iterable

Python-Day4 Python basics advanced Builder/iterator/decorator/Json & pickle data serialization, python-day4json
I. Generator

By using the list generation method, we can directly create a list. However, due to memory restrictions, the list capacity must be limited. In addition, creating a list containing 1 million elements not only occupies a large storage space, but if we only need to access the first few elements, the space occupied by the vast majority of elements is wasted. Therefore, if the list elements can be calculated by some algorithm, can we continue to calculate the subsequent elements in the loop process? In this way, you do not need to create a complete list to save a lot of space. In Python, this type of computing mechanism is called generator.

There are many ways to create a generator. The first method is simple. You only need to generate a list[]Change(), A generator is created:

>>> L = [ x*2 for x in range(5)]>>> L[0, 2, 4, 6, 8]>>> g = ( x*2 for x in range(5) )>>> g<generator object <genexpr> at 0x000000000321EF68>

CreateLAndgOnly the outermost layer[]And(),LIs a list, andgIs a generator. We can print every element of the list directly, but how can we print every element of generator? If you want to print them one by one, you can usenext()The function returns the next value of generator:

>>> next(g)0>>> next(g)2>>> next(g)4>>> next(g)6>>> next(g)8>>> next(g)Traceback (most recent call last):  File "<pyshell#11>", line 1, in <module>    next(g)StopIteration>>> g<generator object <genexpr> at 0x000000000321EF68>>>> g = ( x*2 for x in range(5) )>>> for n in g:    print(n)    02468

Generator stores algorithms and calls them each time.next(g)To calculategUntil the value of the next element is calculated to the last element. If no more elements existStopIteration. Of course, this continuous callnext(g)It's really abnormal. The correct method is to useforLoop, because generator is also an iteratable object. Therefore, after a generator is created, it will never be called.next(), But throughforLoop To iterate it, and do not need to worry aboutStopIteration.

Generator is very powerful. If the calculation algorithm is complexforWhen the loop cannot be implemented, it can also be implemented using functions.

For example, in the famous onacci series, except for the first and second numbers, any number can be obtained by adding the first two numbers:

1, 1, 2, 3, 5, 8, 13, 21, 34 ,...

The Fibonacci series cannot be written in the form of list generation, but it is easy to print it out using functions:

>>> Def fib (max): n, a, B = 0, 0, 1 while n <max: print (B) a, B = B, a + B n = n + 1 return 'done'> fib (10) 11235813213455 'done' ''' observe carefully, the fib function actually defines the calculation rule of the Fibonacci series. It can start from the first element and calculate any subsequent elements. This logic is very similar to generator. That is to say, the above functions and generator are only one step away. To change the fib function to generator, you only need to change print (B) to yield B: ''' >>> def fib (max): n,, B = 0, 0, 1 while n <max: yield B a, B = B, a + B n = n + 1 return 'done'> f = fib (5) >>> f <generator object fib at 0x000000000321EF68 >>> print (next (f) 1 >>> print (next (f) 1 >>> print (next (f )) 2 >>> print (next (f) 3 >>> print (next (f) 5 >>> print (next (f) Traceback (most recent call last): File "<pyshell #49>", line 1, in <module> print (next (f) StopIteration: done

Above fibIn this example, we callyieldWill be interrupted. Of course, you must set a condition for the loop to exit the loop. Otherwise, an infinite number of columns will be generated. Similarly, after the function is changed to generator, we basically never usenext()To obtain the next return value, but directly useforLoop iteration:

>>> For n in fib (5 ):... print (n )... 11235 ''' but when we call generator using a for loop, we find that the return value of the generator return statement is not obtained. If you want to get the returned value, you must capture the StopIteration error. The returned value is included in the StopIteration value: ''' >>> g = fib (5) >>> while True: try: x = next (g) print ('G: ', x) Doesn't StopIteration as e: print ('generator return value:', e. value) break g: 1g: 2g: 3g: 5g: 8 Generator return value: done

The effect of implementing concurrent operations with a single thread through yield: (retained temporarily)

Ii. iterator

Iteration is one of the most powerful functions of Python and a way to access collection elements. An iterator is an object that can remember the traversal position. The iterator object is accessed from the first element of the set until all elements are accessed. The iterator can only move forward without moving back.

To act directlyforThe following types of cyclic data are available:

The first type is the set data type, suchlist,tuple,dict,set,strAnd so on;

FirstgeneratorIncluding the generator andyieldGenerator function.

These can be directly appliedforCyclic objects are collectively referred to as iterative objects:Iterable.

Availableisinstance()Determine whether an object isIterableObject:

>>> from collections import Iterable>>> isinstance([], Iterable)True>>> isinstance({}, Iterable)True>>> isinstance('abc', Iterable)True>>> isinstance((x for x in range(10)), Iterable)True>>> isinstance(100, Iterable)False

The generator can not only act onforLoop, can also benext()The function is continuously called and the next value is returned until it is finally thrown.StopIterationAn error indicates that the next value cannot be returned.

* Can Benext()The object that calls the function and continuously returns the next value is called the iterator:Iterator.

Availableisinstance()Determine whether an object isIteratorObject:

>>> from collections import Iterator>>> isinstance((x for x in range(10)), Iterator)True>>> isinstance([], Iterator)False>>> isinstance({}, Iterator)False>>> isinstance('abc', Iterator)False

All generatorsIteratorObject,list,dict,strAlthough YesIterableBut notIterator.

Setlist,dict,strAnd so onIterableChangeIteratorAvailableiter()Function:

>>> isinstance(iter([]), Iterator)True>>> isinstance(iter('abc'), Iterator)True

You may ask whylist,dict,strAnd other data types are notIterator?

This is because PythonIteratorThe object represents a data stream, and the Iterator object can benext()The function calls and continuously returns the next data until no data is returned.StopIterationError. We can regard this data stream as an ordered sequence, but we cannot know the length of the sequence in advance and can only continue to pass throughnext()Function implementation calculates the next data as needed, soIteratorThe computation of is inert and will only be calculated when the next data needs to be returned.

IteratorIt can even represent an infinitely large data stream, such as all natural numbers. However, using list is never possible to store all natural numbers.

 

Summary

Anything that can act onforLoop objects areIterableType;

Anything that can act onnext()All function objects areIteratorType, which indicates a sequence of inert computing;

The set data type is shown in figurelist,dict,strYesIterableBut notIteratorBut you can useiter()Function to obtainIteratorObject.

PythonforIn essence, a loop is called continuously.next()Function implementation, such:

For x in [1, 2, 3, 4, 5]: pass # is actually equivalent to: # first obtain the Iterator object: it = iter ([1, 2, 3, 4, 5]) # loop: while True: try: # Get the next value: x = next (it) Stopping t StopIteration: # exit the loop break when StopIteration occurs.
3. decorator

After several days of understanding, I began to write the decorator. I first defined that the decorator is essentially a Python function that allows other functions to add additional functions without any code changes, the Return Value of the decorator is also a function object. Suppose we want to enhance the function of a function, for example, to automatically print the time before and after the function call, but do not want to modify the function definition, this way to dynamically add features during code running, it is called a Decorator ).

Def use_logging (func): print ("% s is running" % func. _ name _) # _ name _ Get the function name, that is, bar func () def bar (): print ('I am bar') use_logging (bar) '''execution result: bar is runningi am bar '''

Logic is not hard to understand, but in this case, we need to pass a function as a parameter to the use_logging function every time. In addition, this method has destroyed the original code logic structure. Previously, bar () was run when the business logic was executed, but now it has to be changed to use_logging (bar ). Is there a better way? Of course, the answer is the decorator.

1. No parameter decorations
Import timedef timer (func): def deco (): start_time = time. time () func () stop_time = time. time () print ("The func run time is % s" % (stop_time-start_time) return deco @ timer # equivalent to time1 = timer (time1) def time1 (): time. sleep (1) print ("In the time") time1 () ''' In the timeThe func run time is 1.0000569820404053 '''
2. Parameter decorations
import timedef timer(timeout=0):    def decorator(func):        def wrapper(*args,**kwargs):            start=time.time()            func(*args,**kwargs)            stop=time.time()            print 'run time is %s ' %(stop-start)            print timeout        return wrapper    return decorator@timer(2)def test(list_test):    for i in list_test:        time.sleep(0.1)        print '-'*20,i  #timer(timeout=10)(test)(range(10))test(range(10))
Iv. Json & pickle data serialization

Two modules used for serialization

  • Json, used for conversion between string and python Data Types
  • Pickle, used for conversion between python-specific types and python Data Types

The Json module provides four functions: dumps, dump, loads, and load.

The pickle module provides four functions: dumps, dump, loads, and load.

To be continued ....

 

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.