Python-day4 Python base Advanced generator/iterator/adorner/json & Pickle data serialization

Source: Internet
Author: User
Tags for in range iterable

First, generator

With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted. So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.

There are a number of ways to create a generator. The first method is simple, as long as a list of the generated formula is [] changed () to create a generator:

 for  in range (5)]>>>2, 4, 6, 8] for in range (5)) >>> g<generator object <genexpr> at 0x000000000321ef68>

The L difference between creating and making is g only the outermost [] and, a () L list, and g a generator. We can print out every element of the list directly, but how do we print out every element of generator? If you want to print out one, you can next() get the next return value for generator by using a function:

>>>Next (g) 0>>>Next (g)2>>>Next (g)4>>>Next (g)6>>>Next (g)8>>>Next (g) Traceback (most recent): File"<pyshell#11>", Line 1,inch<module>Next (g) Stopiteration>>>g<generator Object <genexpr> at 0x000000000321ef68>>>> g = (x*2 forXinchRange (5) )>>> forNinchg:Print(n) 02468

Generator saves the algorithm, each time it is called next(g) , computes g the value of the next element until it is calculated to the last element, no more elements are thrown when the StopIteration error occurs. Of course, this constant invocation is next(g) too perverted, and the correct way is to use for loops, because generator is also an iterative object. So, after we create a generator, we basically never call next() it, but iterate over it through the for loop and don't need to be concerned about StopIteration the error.

Generator is very powerful. If the calculated algorithm is more complex, and the loop with similar list generation for cannot be implemented, it can also be implemented with functions.

For example, the famous Fibonacci sequence (Fibonacci), except for the first and second numbers, can be summed up by the top two numbers:

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

The Fibonacci sequence is not written in a list, but it is easy to print it out with a function:

>>>deffib (max): N,a,b= 0,0,1 whilen<Max:Print(b) a B=b,a+B N=n+1return ' Done'>>> FIB (10)11235813213455' Done'" "looking closely, it can be seen that the FIB function is actually a calculation rule that defines the Fibonacci sequence, starting with the first element and extrapolating any subsequent elements, which are actually very similar to generator. In other words, the above functions and generator are only a step away. To turn the FIB function into a generator, simply change print (b) to yield B:" ">>>deffib (max): N,a,b= 0,0,1 whilen<Max:yieldb A, a=b,a+B N=n+1return ' 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): File"<pyshell#49>", Line 1,inch<module>Print(Next (f)) Stopiteration:done

  在上面fibexample, we continue to call in the loop process yield , will continue to interrupt. Of course, you have to set a condition for the loop to exit the loop, or it will produce an infinite sequence. Similarly, after changing a function to generator, we basically never use it next() to get the next return value, but instead use the for loop directly to iterate:

>>> forNinchFIB (5):...     Print(n) ...11235" "However, when you call generator with a For loop, you find that you cannot get the return value of the generator return statement. If you want to get the return value, you must catch the Stopiteration error, and the return value is contained in the value of Stopiteration:" ">>> G=fib (5)>>> whileTrue:Try: x=Next (g)Print('g:', X)exceptstopiteration as E:Print('Generator return value:', E.value) Breakg:1g:2g:3g:5g:8GeneratorreturnValue:done

The effect of implementing concurrent operations in single-threaded scenarios with yield: (temporarily reserved)

Second, iterators

Iteration is one of the most powerful features of Python and is a way to access the elements of a collection. An iterator is an object that remembers where to traverse. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without backing back.

forThere are several types of data that directly act on the loop:

A class is a collection of data types, such as,,, list tuple , and dict set str so on;

One is generator to include the generator and yield the generator function with the band.

These objects, which can be directly applied to for the loop, are called iterative objects: Iterable .

You can use to isinstance() determine whether an object is an Iterable object:

 from Collections Import iterable>>> isinstance ([], iterable) True>>> isinstance ({ }, Iterable) True>>> isinstance ('abc', iterable) True for  in range (), iterable) True>>> isinstance (iterable) False

The generator can not only be used for for loops, but it can also be next() called by the function and return the next value until the last throw StopIteration error indicates that the next value cannot continue to be returned.

* An object that can be called by next() a function and continually returns the next value is Iterator called an iterator:.

You can use to isinstance() determine whether an object is an Iterator object:

 from Collections Import Iterator  for  in range (), Iterator) True>>> isinstance ([], Iterator) False> >> isinstance ({}, Iterator) False>>> isinstance ('abc' ) , Iterator) False

Generators are Iterator objects, but,, list dict str Though Iterable they are, they are not Iterator .

Turn list , dict and str wait for the Iterable Iterator function to be used iter() :

>>> Isinstance (ITER ([]), Iterator) True>>> isinstance (iter ('ABC ' ), Iterator) True

You may ask, why, list dict , str etc. data types are not Iterator ?

This is because the Python Iterator object represents a data stream, and the iterator object can be next() called by the function and will return the next data continuously until there is no data to throw an StopIteration error. You can think of this data stream as an ordered sequence, but we can't know the length of the sequence in advance, only by continuously using the next() function to calculate the next data on demand, so Iterator the calculation is lazy, and it will only be calculated when the next data needs to be returned.

IteratorIt can even represent an infinitely large stream of data, such as the whole natural number. Using list is never possible to store all natural numbers.


Any object that can be used for for the loop is a Iterable type;

All objects that can be used for next() functions are Iterator types, which represent a sequence of lazy computations;

Collection data types such as list , dict ,, and str so on are Iterable not Iterator , however, you can iter() get an object from a function Iterator .

The Python for loop is essentially implemented by calling next() functions, such as:

 forXinch[1, 2, 3, 4, 5]:    Pass#is actually exactly equivalent to:#first get the iterator object:it = iter ([1, 2, 3, 4, 5])#Loop: whileTrue:Try:        #get the next value:x =Next (IT)exceptstopiteration:#exit the Loop if you encounter Stopiteration         Break
Three, the decoration device

Understood for several days, began to write the adorner, first said definition: The adorner is essentially a Python function that allows other functions to add extra functionality without any code changes, and the return value of the adorner is also a function object. Suppose we want to enhance the function of a function, for example, to automatically print time before and after a function call, but do not want to modify the definition of the function, this way of dynamically adding functionality during the run of the code, called "Adorner" (Decorator).

 def   Use_logging (func):  print  ( %s is running   "% func. __name__ ) #  _name_ gets the name of the function, which is bar   Func ()  def   Bar ():  print  (  i am bar    ) use_logging (bar)    execution result: Bar is runningi am bar   

Logically not difficult to understand, but in this case, we have to pass a function as an argument to the Use_logging function every time. And this way has destroyed the original code logic structure, before executing the business logic, run bar (), but now have to change to use_logging (bar). So is there a better way? Of course, the answer is an adorner.

1. Non-parametric adorner
Import TimedefTimer (func):defdeco (): Start_time=time.time () func () Stop_time=time.time ()Print("The func run time is%s"% (stop_time-start_time)) returnDeco@timer#equivalent to Time1=timer (time1)deftime1 (): Time.sleep (1)    Print(" in the time") time1 ()" "In the timethe func run time is 1.0000569820404053" "
2. Parametric decorator
Import TimedefTimer (timeout=0):defDecorator (func):defWrapper (*args,**Kwargs): Start=time.time () func (*args,**Kwargs) Stop=time.time ()Print 'run time is%s'% (stop-start)PrintTimeoutreturnwrapperreturnDecorator@timer (2)defTest (list_test): forIinchList_test:time.sleep (0.1)        Print '-'*20, I#Timer (timeout=10) (test) (range )Test (range (10))
Iv. Json & Pickle Data serialization

Two modules for serialization

    • JSON, used to convert between string and Python data types
    • Pickle for conversion between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load

The Pickle module provides four functions: dumps, dump, loads, load


Python-day4 Python base Advanced generator/iterator/adorner/json & Pickle data serialization

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: 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.