Iterables
with iter(object) methods
- str, list, tuple, set, dict, range
- ___iter__(): returns an iterable_
- __next__(): returns next of iterator_
yield
- will return a generate in a function
Generators
- a special type of iterable
- concise way to construct new iterable object (e.g. iter(<dict>)
- use yield instead of return
gen = (x ** 2 for x in range(100)) def _make_gen(): for x in range(100): yield x ** 2 gen = _make_gen() def squares(n=10): for i in range(1, n+1) yield i ** 2 gen = squares(100) # gen is a generator object for i in gen: print(i)
Variable Compare and Copy
Functional Programming in Python
Functional Programming is treating function like an object/data, a function has
- data type: form domain to range
- definition
- name/ address
In Python, the Function is a first-calss object. Some related usage:
- Lambda Function
- lambda: x, ... :
- decoration
- Partial Argument Application (closure)
- functools.partial(<func_name>,<parameter to fix>(list of dictionary))
- lamda y: f(5,y) -> 5 fixed
- map( <func\lambda>, <container>)
- apply every function to a container
- filter(<func>,<container>)
- reduce(<func>,<container>) (from functools)
Testing
Unit Tests
Document Tests
Coding Styles
PEP8 Coding Style Guid for Python
- reloading module dependencies
- use import lib module
- importlib.reload(<lib>)
- IPython can do dreload
- deep reload
- use import lib module
- keep objects and data alive in if \_name__ == '__main__' _block
- everything you want to expose by %run
- flat better than nexted
- longer files, less jumping around files
- special about python
- longer files, less jumping around files