파이썬을 주언어로 즐겨 사용해왔지만 내장함수나 파이썬만의 표현 방식에 대해서는 잘 알지 못했었다.
특히 종종 다른 사람의 코드를 리뷰하던 중에 이해가 안되는 문법이나 표현을 접하는 경우가 있어 이번 기회에 새로 학습하기로 했다.
“Think like python”1 에서는 파이썬만의 확장된 기능과 문법 등을 통해 파이썬을 좀더 파이썬답게 쓰는 방법에 대해서 설명하였다.
THINK LIKE PYTHON
generally It is important to work with python like python.
There are specific methods and statements in python. Python programmers emphasize simple and legibility using that. This pattern will be helpful for your work and projects.
Check your python version
- Terminal
python --version
python 2.7.8
or python 3.4.3
- Using sys module
Follow PEP 8 guide
PEP2 is guide for the developer to keep in mind to improve productivity and cooperation with others.
-
Whitespace
- Using space instead of tab.
- Use 4 space for Indent having meaningful syntax.
- The maximum length for one sentence is 79.
- If the expression is too long, use the indent (4 space).
- In file, separate function and class using two blank lines.
- In class, method has one blank line for separation.
- List index, call fund, allocating keyword don’t have any space.
- Allocating variable have space before and after it.
a = 1
-
naming
- Functions, variables, attributes follow
lowercase_underscore
- Protected instance attributes follow
_leading_underscore
- Private instance attributes follow
__double_leading_underscore
- Classes and exceptions follow
CapitalizedWord
- Module level constants follow
ALL_CAPS
- In class and instance method, first parameter(for instance itself) denote
self
- In class method, first parameter(for class itself) denote
cls
- Functions, variables, attributes follow
-
Expression and sentences use
-
Use inline positive
if a is not b
instead of negative of positive expressionif not a is b
-
Consider blank value as a
False
-
Consider non-blank value as a
True
-
Differences Byte, str, unicode
Use helper method instead of complicated expression
How to slice sequence
Don’t use start, end, stride in one slice
Use list comprehension instead of map and filter
List comprehension is more effective way comparing filter and map.
Don’t use list comprehension more than two
avoid the case of using more than two comprehension in one list. Consider to make helper or loop.
Use generator with large comprehension
When you save the huge data into the list, you’d better use generator for keeping your memory.
Keep your mind about generator can print out only one time using next()
Use enumerate instead of range
Use zip to process iterator in parallel
There are two problem when you use zip
- In python2, zip is not generator. So you should use
izip
initertools
. - When the length in two list is different, zip doesn’t work well. you can use
zip_longest
initertools
(zip_longest
in python2)
Check the length of the lists before using zip
Don’t use else after repetitive statement like for and while
When will the else
work in the python loop?
This is easy to understand. Never use else
in the loop.
Use benefits of each block in try/except/else/finally
FUNCTION
Function is first basic tool for python developers. To improve readability and make more easily understandable code, it is useful for reuse and refactoring.
Raise exception instead of returning None
How to work close in the variable scope
In the python, function is first class.
Use generator instead of returning list
A generator can save your memory comparing a list when the size is too big.
Defensive iteration
When you use the iterative loop for a iterator, It might be a problem.