Skip to content

Reading Notes: Effective Python

Need to read multiple times:

  • Item 16
  • Item 17
  • Item 20 这个行为default只evaluate一次这个事情值得注意
  • Item 23 这个 __call__这个部分值得注意
  • Item 24 这个polymorphism的概念没有太懂
  • Item 25 也不是特别明白
  • Item 26 Mixin
  • Item 29 @property这个不是很明白——见 Item 30
  • Item 31
  • Item 32
  • Item 34
  • Item 35
  • Concurrency
    • Item 36
    • Item 37
    • Item 39
    • Item 40

Comments:

Object-oriented programming非常不熟悉,尤其是Class, Metaclass相关的内容

Item 1: Know Which Version of Python You’re Using

$ python --version
$ python3 --version
import sys
print(sys.version_info)
print(sys.version)

Comment:

  • prefer python 3

Item 2: Follow the PEP 8 Style Guide

Rules

Whitespace: In Python, whitespace is syntactically significant. Python programmers are especially sensitive to the effects of whitespace on code clarity.

  • Use spaces instead of tabs for indentation.
  • Use four spaces for each level of syntactically significant indenting.
  • Lines should be 79 characters in length or less.
  • Continuations of long expressions onto additional lines should be indented by four extra spaces from their normal indentation level.
  • In a file, functions and classes should be separated by two blank lines.
  • In a class, methods should be separated by one blank line.
  • Don’t put spaces around list indexes, function calls, or keyword argument assignments.
  • Put one—and only one—space before and after variable assignments.

Naming: PEP 8 suggests unique styles of naming for different parts in the language. This makes it easy to distinguish which type corresponds to each name when reading code.

  • Functions, variables, and attributes should be in lowercase_underscore format.
  • Protected instance attributes should be in _leading_underscore format.
  • Private instance attributes should be in __double_leading_underscore format.
  • Classes and exceptions should be in CapitalizedWord format.
  • Module-level constants should be in ALL_CAPS format.
  • Instance methods in classes should use self as the name of the first parameter (which refers to the object).
  • Class methods should use cls as the name of the first parameter (which refers to the class).

Item 3: Know the Differences Between bytes, str, and unicode

In Python 3, there are two types that represent sequences of characters: bytes and str. Instances of bytes contain raw 8-bit values. Instances of str contain Unicode characters. In Python 2, there are two types that represent sequences of characters: str and unicode. In contrast to Python 3, instances of str contain raw 8-bit values. Instances of unicode contain Unicode characters.

Work on UNICODE, and do not assume anything about character encoding. When you’re writing Python programs, it’s important to do encoding and decoding of Unicode at the furthest boundary of your interfaces. The core of your program should use Unicode character types (str in Python 3, unicode in Python 2) and should not assume anything about character encodings.

Helper Functions

#python3
def to_str(bytes_or_str):
    if isinstance(bytes_or_str, bytes):
        value = bytes_or_str.decode('utf-8')
    else:
        value = bytes_or_str
    return value  # Instance of str
def to_bytes(bytes_or_str):
    if isinstance(bytes_or_str, str):
        value = bytes_or_str.encode('utf-8')
    else:
        value = bytes_or_str
    return value  # Instance of bytes
# Python 2
def to_unicode(unicode_or_str):
    if isinstance(unicode_or_str, str):
        value = unicode_or_str.decode('utf-8')
    else:
        value = unicode_or_str
    return value  # Instance of unicode
# Python 2
def to_str(unicode_or_str):
    if isinstance(unicode_or_str, unicode):
        value = unicode_or_str.encode('utf-8')
    else:
        value = unicode_or_str
    return value  # Instance of str

To make this work properly, you must indicate that the data is being opened in write binary mode ('wb') instead of write character mode ('w'). Here, I use open in a way that works correctly in Python 2 and Python 3.

  • In Python 3, bytes contains sequences of 8-bit values, str contains sequences of Unicode characters. bytes and str instances can’t be used together with operators (like > or +).
  • In Python 2, str contains sequences of 8-bit values, unicode contains sequences of Unicode characters. str and unicode can be used together with operators if the str only contains 7-bit ASCII characters.
  • Use helper functions to ensure that the inputs you operate on are the type of character sequence you expect (8-bit values, UTF-8 encoded characters, Unicode characters, etc.).
  • If you want to read or write binary data to/from a file, always open the file using a binary mode (like 'rb' or 'wb').