effictive-python first chapter notes

Chapter One way to think about Pythonic with
1 to confirm python version for their own use (python3)
two mainstream python version: python2 (2020, not maintenance) python3 (recommended)
a variety of popular python Runtime Environment CPython (default) , Jython (the Java)
IronPython, PyPy (the JIT, python2 the effect is significant, python3 almost no effect)
2, follow the style guide PEP8
PEP8 list many details to describe how to write a clear python code.
Blank
with spaces for an indent, NA tab-delimited
and grammar related to each layer is represented by four spaces to indent
characters per line should not be more than 79
occupy multiple lines of long expression, in addition to the first line, the remaining lines required prior to re-level plus four spaces
should file separated by blank lines between two classes and functions
the same class, a method should be used between a blank line spaced from
the table using the acquired list element , or to call the function key parameter assignment when both sides do not add spaces.
a [0]
when the assignment is not variable, the left and right sides of a space should be added, and only a fine write
a = b
named
functions, variables, and attributes should lowercase letters to spell, with the underscore between words
def test_a () , my_name, self.my_name
protected instance properties, should begin with a single underscore
self._leading_underscore
Examples of private property, should begin with two underscores
self .__ double_leading_underscore
classes and exceptions, are the first letter of each word capitalized format
CapitalizedWord
module-level constant, all uppercase
ALL_CAPS
instance method in the class, should be the first parameter called self indicates the object itself
first in class method should be named cls, classes that represent their own
expressions and statements
negative words using inline form, if a is not b instead of if not a is b
not to be judged by the length of the detection method whether the list is empty, if not somelist instead if len (somelist) == 0
is detected somelist whether [1] or "hi" and other non-null value, so should we.
Do not write a single line if statement, for loop, while circulation and except compound statement, but should take these statements into multiple lines to write
import statements should always be placed at the beginning of the file
when the introduction of the module, use an absolute path instead of a relative path
if you have to write a relative path to the import statement should be used from. import foo
file import statement that should be sequentially divided into three parts. Represent the standard library modules, third-party modules, modules for their own use
import sys

import requests

myself Import
. 3, understand bytes, str and unicode distinction
python3 There are two types of sequences of characters and bytes str, the former contains the original 8-bit value
which contains unicode
python3 STR and unicode, str contains the original 8-bit value, comprising unicode unicode
the unicode into a binary file there are many ways, it is common to set encoding to utf-8.
unicode encode binary transfer
binary transfer unicode decode
written in python program when encoding and decoding into the most peripheral interfaces, the core of the use unicode character type.
There are two issues to note
a, python2, if str contains seven unicode and ascii so str seems to be the same type.
In only deal ascii situation, if a function accepts str, you can pass unicode, if you accept unicode, can also pass str. In the python3 str and bytes are not equivalent.
Second, if you use the built-in function to get open file handle .python3 default-8 UTF
, python3 default is binary. Wb adaptation mode can be changed to
4, substituted with a helper to complex expression
if the expression is complicated, it can be disassembled into small pieces and transferred to the auxiliary functions.
5, understand the way the cutting sequence
after cutting the original list, will generate a new list, change the new list does not affect the original list
when the list of assignments, if a slice operation, the original list will be within the range of interest Alternatively value to a new value. Different lengths may be replaced.
6, in the words slicing operation, do not specify start, end, and stride
if stride is negative, consider dismantling it into two assignments. A range of cutting to do, to do a stepping cutting. Or use the built-in module itertools islice
. 7, using the list soon push substituted map and filter (also support collection and dictionaries)
code cleaner
8, more than two lists do not use expressions derivation
9, use a generator expression rewrites large amount of data in the list comprehension
will be changed in parentheses parentheses to the list comprehension instead generator
data is too big, because the list of push to take up too much memory problems.
10, possible substituents range with the enumerate
the enumerate iterator may be packaged as a generator, and returns the index of the element.
11, with two zip function while traversing the iterator
python2 the zip is not the producer, if the list is too large there will be a memory problem
python3 in zip equivalent generator
if the two iterators by length, will exhibit strange behavior.
Traversing the length of the smallest iterator.
itertools built-in module can function parallel zip_longest traverse multiple iterations, a do not care whether the length is equal to
12, do not write back else block for and while loops
13, rational use of each code try \ except \ else \ finally structure block
else block can be used to try to reduce the amount of code, try statement is not an exception occurs during the execution of the request / except spaced code blocks

Guess you like

Origin www.cnblogs.com/lgh344902118/p/11297498.html