Slide 1

Slide 1 text

How to Write Functions in Python Hayao Suzuki PyCon JP Reject Conference 2017 at TECH PLAY SHIBUYA September 6, 2017

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Today’s Theme How to Write Functions in Python Surface-Level Improvements The Power of Keyword Arguments Stateful Functions 3 / 15

Slide 4

Slide 4 text

Surface-Level Improvements I’ve bought this book twice. 4 / 15

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Surface-Level Improvements Improvement Point Follow the PEP 8. Follow the PEP 8 Example def f(ns): sum = 0 for i in ns: sum = sum + i return sum print(f([1, 2, 3, 4, 5])) 6 / 15

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Surface-Level Improvements Improvement Point Docstring, Type hints. Readble Example from typing import List def summation(numbers: List) -> int: """Calculate summation of numbers""" total = 0 for number in numbers: total = total + number return total print(summation([1, 2, 3, 4, 5])) 9 / 15

Slide 10

Slide 10 text

The Power of Keyword Arguments Keyword Args with Default Value Add new behaivors to a function. Default Value from typing import List def summation(numbers: List, modulo: int = 1) -> int: """Calculate summation of numbers""" total = 0 for number in numbers: total = total + number return total % modulo print(summation([1, 2, 3, 4, 5])) # No Change! print(summation([1, 2, 3, 4, 5], modulo=4)) # 15 % 5 = 3 10 / 15

Slide 11

Slide 11 text

The Power of Keyword Arguments Keyword Only Args (Python 3 Only) Force callers to supply keyword args. Keyword Only Args from typing import List def summation(numbers: List, *, modulo: int = 1) -> int: """Calculate summation of numbers""" total = 0 for number in numbers: total = total + number return total % modulo print(summation([1, 2, 3, 4, 5], modulo=5)) # 0 print(summation([1, 2, 3, 4, 5], 5)) # raise TypeError 11 / 15

Slide 12

Slide 12 text

The Power of Keyword Arguments Mutable Default Value Default value is only executed a single time when the function is defined. The Same Timestamp >>> from datetime import datetime >>> from time import sleep >>> def log(message, when=datetime.now()): ... print(f"{message}: {when}") ... >>> log("Good Morning!") Good Morning!: 2017-09-03 22:20:40.786377 >>> sleep(1.0) >>> log("͓͸Α͏͍͟͝·͢") ͓͸Α͏͍͟͝·͢: 2017-09-03 22:20:40.786377 12 / 15

Slide 13

Slide 13 text

The Power of Keyword Arguments Mutable Default Value Use None to Mutable Default Value. The Correct Timestamp >>> from datetime import datetime >>> from time import sleep >>> def log(message, when=None): ... when = datetime.now() if when is None else when ... print(f"{message}: {when}") ... >>> log("Good Morning!") Good Morning!: 2017-09-03 22:20:41.786424 >>> sleep(1.0) >>> log("͓͸Α͏͍͟͝·͢") ͓͸Α͏͍͟͝·͢: 2017-09-03 22:20:42.787629 13 / 15

Slide 14

Slide 14 text

Stateful Functions Stateful Function Implement callable class. Implements Summation with Histories class Summation: def __init__(self): self.histories = list() def __call__(self, numbers, *, modulo=1): total = sum(numbers) % modulo self.histories.append((numbers, modulo, total)) return total summation = Summation() print(summation([1, 2, 3, 4, 5])) print(summation([1, 2, 3], modulo=3)) print(summation.histories) 14 / 15

Slide 15

Slide 15 text

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