Python derived expressions, iterators, generators, modules and packages

  1. Derive an expression
  2. yield Usage
  3. Module concept and method for introducing
  4. Packages and package management

Derived expression (using for, one by one into data )

List comprehension

 

Derivation collection

 

Derivation dictionary

 

 

 

Iterator

Iteration

for the iteration variable in iterable

Each cycle will automatically let 'iteration variable "points" to the next element. " 

 

The method of generating iterator

travel () __iter __ ()

 

The value

next(iterator)      iterator.__next__()

Note: If, after the iterator value to take complete, it will return an error StopIteration

 

Iterator object itself needs to support the following two methods, which together form the iterator protocol

iterator.__iter__()    iterator.__next__()

 

Generates an iterator from iterables

Iterator = iter (iterables)

The next value = next (iterator)

 

for implementation principle

 

Custom iterator

 

 

 

Builder

Builder will not suddenly put it out of content generation, () with the next generation to use when needed

Method a: deriving a list of the formula [] into ()

 

Method two: function inside with yield

 

yield operating rules

yiled an object 1, return the object 2, 3 pause function, wait for the next next Reactivate

 

note

yield expression can only be used in the function 

yield expression can function as a generator

yield expression can return a result, and execute tentative function, until the next activation yield a

Simply understanding generator is a iterator

 

Python using generators to delay operations provide support
so-called latency operations, refers only produce results when needed, rather than produce immediate results, thus saving a lot of space, which is the generator of the main benefits

 

 

 

Modules and packages

There are three methods of introducing

import package name

from a package name import module name, module name 2, ...

In the same directory, the above two methods can be used to directly introducing

In different directories, use sys.path.append ( 'path') adding the path, the path is added to the sys.path

 

Module

In python, the module is a py file

 

Packages and package management

Package concept: many modules into the inside of a folder, a package can be formed

Package Manager: When the many modules in a file, in order to facilitate package module references, the introduction of package management

 

__init__.py

In package management added this module, the packet attribute name can be directly accessed through the access object within this module

python2 must be added, pyhton3 can also be used without the

Specification is to add the file content may be empty

 

Introducing relative path

In the package management, respectively, can be introduced into the same layer and the upper layer through the module. (A dot) and .. (two points)

Usage: from .module (.. module) import obj 

Introducing effect: in a package, if the package module to import other modules in the same package, it is necessary to use this method for introducing

After introduction Effect: When this occurs a manner import module, the module can not be run directly, it can be introduced into

 

Guess you like

Origin www.cnblogs.com/jiyu-hlzy/p/11770725.html