Python as a Foreign Language

by @dankogai

Prologue

pyconjp2012 http://2012.pycon.jp の2日目(9/16)に講演をお願いしたいのですがどうでしょうか?

the-truth-of-gunma13

puellae.map(lang)

puellae.map(lang)

puellae.map(lang)

Perlの補足

tc1.search.naver.jp Then
use strict;
use warnings;
Now
use v5.12; # or better

The Three Virtues of a Programmer

220px-Larry_Wall_YAPC_2007
  1. laziness(怠惰)
  2. Impatience(短気)
  3. Hubris(傲慢)

1. Lazyness(怠惰)

The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful, and document what you wrote so you don't have to answer so many questions about it.

全体の労力を減らすために手間を惜しまない気質。この気質の持ち主は、役立つプログラムを書いてみんなの苦労を減らしたり、同じ質問に何度も答えなくてもいいように文書を書いたりする。

1. Lazyness(怠惰)→Perl?

2. Impatience(短気)

The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least that pretend to.

コンピューターが怠慢な時に感じる怒り。この怒りの持ち主は、今ある問題に対応するプログラムにとどまらず、今後起こりうる問題を想定したプログラムを書く。少なくともそうしようとする。

2. Impatience(短気)→Ruby?

3. Hubris(傲慢)

Excessive pride, the sort of thing Zeus zaps you for. Also the quality that makes you write (and maintain) programs that other people won't want to say bad things about.

神罰が下るほどの過剰な自尊心。または人様に対して恥ずかしくないプログラムを書き、また保守しようとする気質。

3. Hubris(傲慢)→Python!

Zen of Python

import this
Beautiful is better than ugly.
きたないのよりきれいな方がいい
Perl
join "☆" => qw/まどか マギカ/;
Ruby
%w[まどか マギカ].join('☆')
JavaScript
['まどか', 'マギカ'].join('☆');
Python
u"☆".join(['まどか', 'マギカ'])
Explicit is better than implicit.
ごちゃごちゃ難しいのより、白黒はっきりしてるのがいい
Explicit is better than implicit.
🐍
Almost anybody
"🐍"
Python 2
u"🐍"
Python 3
"🐍"
Python 3.3
u"🐍"
"🐍"
from __past__ import unicode
# わけがわからないよ
Perl
"\x{1F40D}"
Ruby
"\u{1F40D}"
Python (and C++11)
u"\U0001F40D"
Simple is better than complex.
めんどうなのよりかんたんな方がいい

A Little Wish

madoka-wishes

Perl
$universe =~ s/魔女//g;
Ruby
universe.gsub(/魔女/, '');
JavaScript
universe.replace(/魔女/g, '');
Python
import re;
re.compile('魔女').sub('', universe);
Complex is better than complicated.
けど、訳分かんなくなるくらいならめんどうな方がまし
Perl
sub make_counter {
    my $count = shift;
    sub { return ++$count }
}
my $c0 = make_counter(0);
my $c1 = make_counter(0);

print $c0->(), "\n";
print $c1->(), "\n";
print $c1->(), "\n";
print $c0->(), "\n";
print $c1->(), "\n";
print $c1->(), "\n";
Python - Buggy
def make_counter(count):
    def fun():
        count += 1
        return count
    return fun

c0 = make_counter(0)
c1 = make_counter(100)

print(c0())
print(c1())
print(c1())
print(c0())
print(c1())
print(c1())
Python - Corrected
def make_counter(count):
    def fun():
        nonlocal count
        count += 1
        return count
    return fun

c0 = make_counter(0)
c1 = make_counter(100)

print(c0())
print(c1())
print(c1())
print(c0())
print(c1())
print(c1())
Flat is better than nested.
ネストさせなくていいならしない方がいい
Flat is better than nested.
Sparse is better than dense.
たくさん詰め込んだのよりスカスカな方がいい
Sparse is better than dense.
Sparse is better than dense.
asin:4873115655 Readability counts.
読みやすさがたいせつなのよ
Readability counts.
→ We'll talk about it later
Special cases aren't special enough to break the rules.
特別なこともあるけど掟破りってほどじゃない
Special cases aren't special enough to break the rules.
Although practicality beats purity.
実用性を求めてくと、ちょっとはずれちゃうこともあるけどね
Although practicality beats purity.
→ We'll talk about it later
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.
間違えようのないやり方がひとつだけあるのがいいね
There should be one-- and preferably only one --obvious way to do it.
len(u"\U0001F40D")
There should be one-- and preferably only one --obvious way to do it.
% /usr/bin/python
Python 2.7.2 (default, Jun 20 2012…
[GCC 4.2.1 Compatible Apple Clang 4.0…
Type "help", "copyright", "credits"…
>>> len(u"\U0001F40D")
2
There should be one-- and preferably only one --obvious way to do it.
% port installed |grep python
  python27 @2.7.3_0+ucs4 (active)
  python32 @3.2.3_0+ucs4 (active)
  …
% /opt/local/bin/python2.7
Python 2.7.3 (default, Aug  9 2012…
[GCC 4.2.1 Compatible Apple Clang 4.0…
Type "help", "copyright", "credits"…
>>> len(u"\U0001F40D")
1
Although that way may not be obvious at first unless you're Dutch.
オランダ人以外には、ちょっと分かりにくかったりしてもね
Although that way is not be obvious for years unless you're stuck with the Basic Multilingual Plane.
BMPで満足しちゃっている限りは
Now is better than never.
やらないよりは今やるべき
Although never is often better than *right* now.
けど今「すぐ」やるならやんない方がいいこともある

u"🐍"
or
just "🐍"

If the implementation is hard to explain, it's a bad idea.
作るものをうまく説明できないようならそれはボツ
If the implementation is hard to explain, it's a bad idea.
% ./configure  --enable-unicode=ucs4
If the implementation is hard to explain, it's a bad idea.
% ./configure  --enable-unicode=ucs4
Python 3.3 で直るのですよね?
If the implementation is easy to explain, it may be a good idea.
うまく説明できたらたぶんそれがグッド
If the implementation is easy to explain, it may be a good idea.
Guido_van_Rossum_OSCON_2006

Would somebody explain this mess to the Dutch in Mountain View?
どなたかマウンテンビューのオランダ人に説明してあげてくだちい

Namespaces are one honking great idea -- let's do more of those!
ぶらぼーなアイディア名前空間、やっぱこれですね
Namespaces are one honking great idea -- let's do more of those!
% easy_install
何をeasy_installするって?
Perlも人のこと言えた義理か?
% prove
何をproveするって?
(まあTAPは言語非依存ではありますが)

Why Python is not as popular here as it should be?

なぜPythonがいまいち萌えてないかって?

Because you are not importing enough!

import足りなさすぎ!

Python deserves more.

We deserve more from Python.

Python to Foreigners = Readablity

asin:4873115655

So fix the unicode mess

emoji

Another Little Wish

「すべての疑似コードを、生まれる前に消し去りたい」

madoka-wishes

Errors should never pass silently.
→ The most especially if the error is in the language itself.
220px-Larry_Wall_YAPC_2007 "No language is an island"
「島言語なんて、あるわけない」

Zen is not about reading mantra.
Zen is all about practice.

禅は唱えるべきものではありません
禅は為すべきものです

御礼なう

for question in questions:
    question.answer