Professional Documents
Culture Documents
Hayao Suzuki
September 6, 2017
Who Am I?
About Me
Name Hayao Suzuki (@CardinalXaro)
Blog http://xaro.hatenablog.jp/
Major Mathematics (Combinatorics, Number Theory)
Work Python Programmer at iRidge, Inc.
Reviewed Books
Effective Python (O’Reilly Japan)
アルゴリズムクイックリファレンス 第 2 版 (O’Reilly Japan)
初めての PHP (O’Reilly Japan)
Effective Debugging (O’Reilly Japan)
すらすらわかる Python (翔泳社)
Python と JavaScript ではじめるデータビジュアライゼーション
(O’Reilly Japan)
2 / 15
Today’s Theme
3 / 15
Surface-Level Improvements
4 / 15
Surface-Level Improvements
Ugly Example
def f(ns):
sum=0
for i in ns:
sum=sum+i
return sum
print(f([1,2,3,4,5]))
5 / 15
Surface-Level Improvements
Improvement Point
Follow the PEP 8.
print(f([1, 2, 3, 4, 5]))
6 / 15
Surface-Level Improvements
Improvement Point
Consider namespace (e.g. sum is a builtin-function).
sum ! total
def f(ns):
total = 0
for i in ns:
total = total + i
return total
print(f([1, 2, 3, 4, 5]))
7 / 15
Surface-Level Improvements
Improvement Point
Packing Iinformation into names.
Improve names
def summation(numbers):
total = 0
for number in numbers:
total = total + number
return total
print(summation([1, 2, 3, 4, 5]))
8 / 15
Surface-Level Improvements
Improvement Point
Docstring, Type hints.
Readble Example
from typing import List
print(summation([1, 2, 3, 4, 5]))
9 / 15
The Power of Keyword Arguments
Default Value
from typing import List
10 / 15
The Power of Keyword Arguments
11 / 15
The Power of Keyword Arguments
12 / 15
The Power of Keyword Arguments
13 / 15
Stateful Functions
Stateful Function
Implement callable class.
summation = Summation()
print(summation([1, 2, 3, 4, 5]))
print(summation([1, 2, 3], modulo=3))
print(summation.histories)
14 / 15
Summary
Summary
If you don’t have "The Art of Readable Code", go to a
bookstore and buy it.
Follow the PEP 8. Consider built-in namespaces.
Keyword Arguments are very powerful. Be careful of mutable
default values.
If you want to implement stateful functions, we recommend
callable class instead of closures.
References
リーダブルコード (O’Reilly Japan)
Effective Python (Addison-Wesley)
15 / 15