题目描述
Python专属 笔记
1. enumerate: `for i, a in enumerate(A)`
2. zip: `for a, b in zip(A, B)`
3. list comprehension `[x for x in a]`
4. generator `(x for x in a)`
5. try/except/else/finally:
6. not use `None` as the return, try `Exception`
7. `nonlocal`, `global`: The nonlocal statement makes it clear when data is being assigned out of closure into another scope. It’s complementary to the global statement, which indicates that a variable’s assignment should go directly into the module scope. Anyway, try to use warp the function to a `Class`
8. Consider Generators Instead of Returning Lists: reduce memory cost and fit for big data `yield`. Can be simply warped to list by `list(generator)`
9. build up `__iter__()` as a generator in class
10. `*args`: optional arguments; fit for a small number of args
11. `assert`
12. Keyword arguments: default value, position. Default argument values are evaluated only once per module load, which usually happens when a program starts up.
13. Docstrings: describe the function, then the `args` and `return`
14. Using `None` for default argument values is especially important when the arguments have a dynamic value.
15. **The * symbol in the argument list indicates the end of positional arguments and the beginning of keyword-only arguments.** [之前没考虑过]
16. `@classmethod` & `@staticmethod`
17. `NotImplementedError`
18. Python only supports a single constructor per class, the `__init__` method.
19. Always use the `super` built-in function to initialize parent classes.
20. Use `@property` to define special behavior when attributes are accessed on your
objects, if necessary.
21. decorator: for allowing one function to modify another function
at runtime.
22. `logging` package.
23. `pickle.dump() pickle.load()`
24. represent time: `datetime` duration: `time.time()`
25. `__init__.py` for path initialization: Once `__init__.py` is present, any other Python files in that directory will be available for import using a path relative to the directory.
26. `pdb`