Generator, iterator, JSON

Source: Internet
Author: User

First, generator

The data type that can be traversed by for is an iterative type, which can be iterated by next as an iterator. such as the list

The generator does not generate elements when it is not called. A new element is generated only by a call, such as a For loop, or next ().

Example

c= (i*2 for I in range) for I in    C:        print (i)

This is a generator.

Functions can be converted to generators by yield

def sal (max):    n=0 while    N<max:        yield n        n+=1

This is also a generator

Second, iterators

You can use next to get the current element and return to the current state

DEF sport: While    True:        project=yield        Print ("Start%s Sport"%project) C1=sport () C2=sport ()

Iterators can emit a value by sending () to yield and return to the next state,

Concurrent effects can be achieved via send

Third, JSON

Serialization and deserialization of Json.dumps and Json.loads

JSON can only be used for relatively simple data types: Lists, dictionaries, these

Complex data, such as functions, must be serialized by means of pickle.

Serialization and deserialization of Pickle.dumps and Pickle.loads

Generator, iterator, JSON

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.