Generator
In Python, a mechanism that loops one side of the computation, called the generator: Generator.
Such as:
1 for Xin range2 >>> g
<generator
object
<genexpr> at
0x1022ef630
>
Here g is a generator.
Iterators
We already know that for
there are several types of data that can be directly acting on a 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:
1>>> fromCollectionsImportiterable2>>>isinstance ([], iterable)3 True4>>>isinstance ({}, iterable)5 True6>>> Isinstance ('ABC', iterable)7 True8>>> Isinstance ((x forXinchRange (10) ), iterable)9 TrueTen>>> isinstance (100, iterable) OneFalse
* An object that can be called by next()
a function and continually returns the next value is Iterator
called an iterator:.
Generators are Iterator
objects, but,, list
dict
str
Though Iterable
they are, they are not Iterator
.
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
.
Decorative Device
The function of the adorner fully conforms to the open-closed principle in the development. Without changing the original function code, do not change the original call mode, realize the expansion of new functions.
Two methods for module import
Import Auth
Import auth as OS take individual name OS
From auth Import Login
__file__ the file name of the relative path
Os.basename (Os.path.abspath (__file__)) converts a relative path to an absolute path
Py2
There is no __init__ in the directory, it is just a directory. Directories are not allowed to be imported.
There is __init__. Then this directory becomes a "package" = Packages
It's just inside the py3. Directories can be imported without adding __init__
Common Module OS Modules
Provides an interface to invoke the operating system
1 OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work2Os.chdir ("dirname") Change the current script working directory, equivalent to the shell CD3Os.curdir returns the current directory: ('.')4Os.pardir Gets the parent directory string name for the current directory: ('..')5Os.makedirs ('dirname1/dirname2') to generate a multi-level recursive directory6Os.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on7Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir dirname8Os.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error; equivalent to the shell rmdir dirname9Os.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints as a listTen os.remove () delete a file OneOs.rename ("oldname","newname") Rename File/Catalogue AOs.stat ('Path/filename') Get File/directory information -OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/" -OS.LINESEP output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n" the os.pathsep output string for splitting the file path -The Os.name output string indicates the current usage platform. Win->'NT'; Linux->'POSIX' -Os.system ("Bash Command") to run the shell command to display directly - Os.environ getting system environment variables + Os.path.abspath (path) returns the absolute path normalized by path - os.path.split (path) splits path into directory and file name two tuples returned + os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path) A os.path.basename (path) returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path) at os.path.exists (path) returns true if path exists, false if path does not exist - os.path.isabs (path) returns True if path is an absolute path - os.path.isfile (path) returns True if path is a file that exists. Otherwise returns false - os.path.isdir (path) returns True if path is a directory that exists. Otherwise returns false - Os.path.join (path1[, path2[, ...]) When multiple paths are combined, the parameters before the first absolute path are ignored - os.path.getatime (path) returns the last access time of the file or directory to which path is pointing inOs.path.getmtime (Path) returns the last modified time of the file or directory to which path is pointing
SYS module
1 sys.argv command line argument list, the first element is the program itself path 2sys.exit (n) exit the program, exit normally exits (0)3 Sys.version Gets the version information of the Python interpreter 4sys.maxint the largest int value 5Sys.path returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing 6 Sys.platform Returns the operating system platform name
JSON & Pickle Modules
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 Learning Notes (5)--iterators, generators, decorators, common modules, serialization