simple is better than complex. ~私がpythonを愛する理由~

37
SIMPLE IS BETTER THAN COMPLEX

Upload: cocodrips

Post on 08-Feb-2017

534 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Simple is better than complex. ~私がPythonを愛する理由~

SIMPLE IS BETTER THAN COMPLEX

Page 2: Simple is better than complex. ~私がPythonを愛する理由~

@cocodrips

Page 3: Simple is better than complex. ~私がPythonを愛する理由~

知らない言語を初めて学ぶ時、 まず何をしますか?

Page 4: Simple is better than complex. ~私がPythonを愛する理由~

まずそのプログラミング言語の 言語思想を学ぶ

Page 5: Simple is better than complex. ~私がPythonを愛する理由~

Perl 「There’s More Than One Way To Do It.」

Page 6: Simple is better than complex. ~私がPythonを愛する理由~

use English; if ($INPUT_LINE_NUMBER >= 1 and $INPUT_LINE_NUMBER <= 3 or $ARG =~ m/match/) { print $ARG; }

if (1..3 or /match/) { print }

print if 1..3 or /match/

Page 7: Simple is better than complex. ~私がPythonを愛する理由~

まつもとゆきひろさん(Ruby) 「気分よく書けるのが1番良い」

Page 8: Simple is better than complex. ~私がPythonを愛する理由~

The Zen of Python

Page 9: Simple is better than complex. ~私がPythonを愛する理由~
Page 10: Simple is better than complex. ~私がPythonを愛する理由~

The Zen of Python, by Tim Peters !Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!

Page 11: Simple is better than complex. ~私がPythonを愛する理由~

Beautiful is better than ugly.

Page 12: Simple is better than complex. ~私がPythonを愛する理由~

Explicit is better than implicit.

Page 13: Simple is better than complex. ~私がPythonを愛する理由~

Simple is better than complex.

Page 14: Simple is better than complex. ~私がPythonを愛する理由~

Complex is better than complicated.

Page 15: Simple is better than complex. ~私がPythonを愛する理由~

Flat is better than nested.

Page 16: Simple is better than complex. ~私がPythonを愛する理由~

Sparse is better than dense.

Page 17: Simple is better than complex. ~私がPythonを愛する理由~

if i>0: return sqrt(i) elif i==0: return 0 else: return 1j * sqrt(-i)

Page 18: Simple is better than complex. ~私がPythonを愛する理由~

if i>0: return sqrt(i) elif i==0: return 0 else: return 1j * sqrt(-i)

if i > 0: return sqrt(i) elif i == 0: return 0 else: return 1j * sqrt(-i)

Page 19: Simple is better than complex. ~私がPythonを愛する理由~

Readability counts.

Page 20: Simple is better than complex. ~私がPythonを愛する理由~

Special cases aren't special enough to break the rules.

Page 21: Simple is better than complex. ~私がPythonを愛する理由~

Although practicality beats purity.

Page 22: Simple is better than complex. ~私がPythonを愛する理由~

Errors should never pass silently. Unless explicitly silenced.

Page 23: Simple is better than complex. ~私がPythonを愛する理由~

try: import this except ImportError: pass !

try: import this except ImportError: print('this is not available')

Page 24: Simple is better than complex. ~私がPythonを愛する理由~

In the face of ambiguity, refuse the temptation to guess.

Page 25: Simple is better than complex. ~私がPythonを愛する理由~

There should be one — and preferably only one —

obvious way to do it.

Page 26: Simple is better than complex. ~私がPythonを愛する理由~

Although that way may not be obvious at first unless you're Dutch.

Page 27: Simple is better than complex. ~私がPythonを愛する理由~

Now is better than never.

Page 28: Simple is better than complex. ~私がPythonを愛する理由~

with open('will_be_closed.txt', 'w') as f: f.write('the sum of two primes > 2 is an even number') raise AssertionError('this sentence is false')

f = open('stay_open.txt', 'w') f.write('every even number > 2 is the sum of two primes') assert not 'this sentence is false' f.close()

Never:

Now:

Page 29: Simple is better than complex. ~私がPythonを愛する理由~

Although never is often better than *right* now.

Page 30: Simple is better than complex. ~私がPythonを愛する理由~

If the implementation is hard to explain, it's a bad idea.

!

If the implementation is easy to explain, it may be a good idea.

Page 31: Simple is better than complex. ~私がPythonを愛する理由~

Namespaces are one honking great idea -- let's do more of those!

Page 32: Simple is better than complex. ~私がPythonを愛する理由~

from module import *

Page 33: Simple is better than complex. ~私がPythonを愛する理由~

import module as mo

Page 34: Simple is better than complex. ~私がPythonを愛する理由~

おすすめの本言語設計者たちが考えること

Page 35: Simple is better than complex. ~私がPythonを愛する理由~

課題

import thisをして、dir(this)をしてみましょう。

thisの中にあるオブジェクトを用いて、import thisした時に出てくる文章を作ってみましょう。

Page 36: Simple is better than complex. ~私がPythonを愛する理由~

The Zen of Python

Page 37: Simple is better than complex. ~私がPythonを愛する理由~

• 我々は「Python」に何を求めているのか? - gumi Engineer’s

Blog - http://d.hatena.ne.jp/gumilab/20120229/1330479257 !

• プログラマが持つべき心構え (The Zen of Python) - Qiita -

http://qiita.com/IshitaTakeshi/items/e4145921c8dbf7ba57ef” !

• There's more than one way to do it - Wikipedia, the free encyclopedia - http://en.m.wikipedia.org/wiki/There's_more_than_one_way_to_do_it