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

Preview:

Citation preview

SIMPLE IS BETTER THAN COMPLEX

@cocodrips

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

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

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

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/

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

The Zen of 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!

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.

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)

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

Readability counts.

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

Although practicality beats purity.

Errors should never pass silently. Unless explicitly silenced.

try: import this except ImportError: pass !

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

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.

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:

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!

from module import *

import module as mo

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

課題

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

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

The Zen of 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

Recommended