read

파이썬을 주언어로 즐겨 사용해왔지만 내장함수나 파이썬만의 표현 방식에 대해서는 잘 알지 못했었다.
특히 종종 다른 사람의 코드를 리뷰하던 중에 이해가 안되는 문법이나 표현을 접하는 경우가 있어 이번 기회에 새로 학습하기로 했다.
“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

  1. Terminal
python --version

python 2.7.8 or python 3.4.3

  1. Using sys module

Follow PEP 8 guide

PEP2 is guide for the developer to keep in mind to improve productivity and cooperation with others.

  1. 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
  2. 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
  3. Expression and sentences use

    • Use inline positive if a is not b instead of negative of positive expression if 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

  1. In python2, zip is not generator. So you should use izip in itertools .
  2. When the length in two list is different, zip doesn’t work well. you can use zip_longest in itertools(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.


To make clear, use variable position argument

Offer optional action by using keyword argument

Use None and docstring for dynamic basic argument

Emphasize the clearness of argument for keyword.

Use helper class

Use function instead of class for argument

  1. “Effective python 59: specific ways to write better python” in English, 파이썬 코딩의 기술 

  2. Python Enhancement Proposal. It is style guide about how to construct python code. 

Blog Logo

Taekyung Han


Published

Image

SHEPHEXD

CAN MACHINES THINK LIKE HUMANS?

Back to Overview