ãã¾ãããªpythonã®ã¡ã¢
ä»äºã¨é¢ä¿ãªããã¨ããã¦ããªãã¨ãããã¨ã¯ãä»äºä»¥å¤ãã¦ããªãã¨ãããã¨ã¨åãã§ããããããªäººçã¯ããã ãã¨ããããã§ãã«ã¼ã¿ãã¹ã¤ããããé¢ãã¦pythonæ¸ãã¦ãã¨ããªã©ã¯ãmotieã¯ITå±ã§ãããã¤ä»äºä¸ã§ãªãããæãç«ã¤è²´éãªç¬éã
ã¾ãã«å¤§å¦ã®ãµã¼ãã«ä¸ãã¦ããwikiã®ã¡ã¢æ¸ããæ¸ãç´ãã¦ä¸ãã¦ããã*1ã
*1:ããããã¨ãã£ã¦ããåè¨äºãåå¥ã®ã¿ã¤ãã«ã«ãªã£ã¦ãã¾ã£ãããªã³ã¯ã§é£ã¶ã«ã¯ã¾ãæ¥ä»ãªã³ã¯ãè¸ã¾ãªãããããªãä»æ§ã«ãwikiã®ä»£ããã«ããã®ã¯ãªããªãé£ããã
ã¯ã¦ãªãã¤ã¢ãªã¼ã®syntax highlight
pythonã¨ã¯ç´æ¥é¢ä¿ãªãã±ããはてなダイアリーはシンタックスハイライトの機能を実装しているã
>|python|
# -*- coding: utf-8 -*-
def main():
print "hello, spam!"
return 1if __name__=="__main__":
import sys
sys.exit(main())
< |
ã©ãã©ã
# -*- coding: utf-8 -*- def main(): print "hello, spam!" return 1 if __name__=="__main__": import sys sys.exit(main())
ãããã§ããã
The Zen of Python, by Tim Peters
pythonãã¤ã³ã¹ãã¼ã«ãããã¾ãããããã«import thisããã¹ã*1ã
>>> import this
The Zen of Python, by Tim PetersBeautiful 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.
(ééããã¨åããã«ããã£ãããã¦ãã)
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!
(ã¶ãã¼ã¼ãªã¢ã¤ãã£ã¢åå空éããã£ã±ããã§ãã)
*1:å訳é¨ã¯http://www.python.jp/Zope/articles/misc/zenããã
japãã¾ãã¯ããã«ãã¹ããã¨ï¼sitecustomize.pyã®ç·¨éï¼
pythonã®è¨èªè¨å®ãutf-8ã¨ãã«ãã¾ãï¼å¿ é ã§ã¯ãªããã©æåã³ã¼ãå¦çã§æãæããã®ã§ãã£ã¦ãããã»ããããã¨æãããï¼ãã¾ãpython -vã§ã©ã®ãã£ã¬ã¯ããªã®ã©ã¤ãã©ãªãèªã¾ãã¦ããã調ã¹ãã
~ $ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib/python2.5/site.pyc matches /usr/lib/python2.5/site.py
ï¼ä»¥ä¸ç¥ï¼
ãã®ä¾ã§ã¯/usr/lib/python2.5ãèªã¾ãã¦ãããããã®ã§ããã®ä¸ã®site-packages以ä¸ã«sitecustomize.pyã¨ãããã¡ã¤ã«ãä½ãutf-8ãããã©ã«ãã§ä½¿ç¨ããããã«ãããï¼windowsã ã¨c:\python25\lib\site-packages\sitecustomize.pyã¿ãããªãã¡ã¤ã«ã«ãªãã¨æãã¾ãï¼ãã¨ãã°rootã§ä»¥ä¸ã®ã³ãã³ããæã¤ã
localhost ~ # echo "import sys; sys.setdefaultencoding('utf-8')" > /usr/lib/python2.5/site-packages/sitecustomize.py
ãããã¯ä»¥ä¸ã®å 容ã/usr/lib/python2.5/site-packages/sitecustomize.pyã¸æ¸ãè¾¼ãã
import sys
sys.setdefaultencoding("utf-8")
ã¤ã³ã¿ã©ã¯ãã£ãã·ã§ã«ãã以ä¸ã®ãããªçµæãå¾ãããã°OKã
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'