2010/09/13

Pythonのルール まとめ

Release:1.1
Date:September 20, 2010
自分用に書いた、Pythonの基本的なルールのまとめです。
間違いや勘違いが多分に含まれている可能性があるので、参考にされる方は
公式のドキュメントなどで確認をお願いします。

– インタプリタを起動する

Pythonインタブリタを起動するには単純にシェル上でpythonとコマンドを打ちます。

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

インタプリタの終了は、Unixでは Control-D 、Windows では Control-Z
もしくは、

>>> quit()

さらには、

>>> import sys
>>> sys.exit()

でも、終了できます。

もし、Pythonのインタプリタを操作していて入力を受け付けなくなった場合は、
Control-CKeyboardInterrupt を発生させて終了させます。
>>> while True:
...     pass
...
^C
KeyboardInterrupt

Pythonインタプリタには、IPythonやbpythonなど便利なものがあります。

– Python スクリプト ファイルの作成

Python スクリプトをシェルスクリプトのようにして直接実行可能にするにはスクリプトの
先頭に以下の行を置きます。
#!/usr/bin/env python

#! はファイルの最初の2文字でなければなりません。

このほかにも、必要に応じて

#!/usr/bin/pytohn
#!/usr/local/bin/python

などとすることもできます。

実行するときは、以下のようにするか

$ python myscript.py

あるいは、 chmod コマンド などを用いて、スクリプトに実行権限を与えます。

$ chmod +x myscript.py

実行権限が与えられている場合には直接実行できます。

$ ./myscript.py
Hello, World!

– ソースコードの文字コード方式 (encoding)

Python2系では、ASCⅡ 形式の文字コードが採用されているため、
ASCII 形式でない文字コード化方式(エンコーディング)をソースファイルの中で使うには、
#! 行の 直後に一行かそれ以上の特殊なコメントを挿入して、ソースファイルのエンコード
を指定しなければなりません。
# -\*- coding: encoding -\*-
このように宣言しておくと、ソースファイル中の全ての文字は encoding という文字コードでエンコードされているものとして扱われ、Unicode 文字列リテラルを指定したエンコードで直接記述できます。

UTF-8でソースコードを記述する場合は、下記のように宣言します。

# -*- coding: utf-8 -*-

string = u"あ"
print ord(string)

Windowsの場合は、cp932を宣言します。

# coding: cp932

moji = u"あ"
print ord(moji)

宣言をしていなければ、ソースコード中に半角英数字以外の文字を記述すると以下のエラーがでます。

$ python string.py
File "string.py", line 7
SyntaxError: Non-ASCII character '\xe3' in file string.py on line 7, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

– デフォルトエンコーディング

Python2系では、デフォルトのエンコーディングは常にASCⅡに設定されています。
そのため、日本語などのUnicode文字をstr()で変換しようとすると下記のようなエラーが発生します。

>>> uni = u'あ'
>>> str(uni)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u3042' in position 0: or
dinal not in range(128)

これを解決するには、そのつどエンコードしなければなりません。

str(uni.encode('utf-8', 'replace'))
'\xe3\x81\x82'
デフォルトのエンコーディングを変更するためにはsitecustomize.pyを作成するか
Pythonのパス上にあるsitecustomize.pyに追記しなければなりません。
Ubuntu10.04の場合は、 /usr/lib/python2.6 もしくは、 /etc/python2.6/
sitecustomize.pyに以下を記述します。
import sys
sys.setdefaultencoding(sys.getfilesystemencoding())

Windowsの場合は、 C:\Python26\Lib\ に作成しなければならないようですがわかりません。

デフォルトのエンコーディングは以下のようにして確認できます。

>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
$ Python -S hoge.py

のように -S オプションを付けるとスクリプトファイル内に書いても実行できます。

このことを利用して、スクリプトファイル内に、以下のように記述してもできますが
あまりおすすめしません。
#!/usr/bin/python -S
import sys
sys.setdefaultencoding('utf-8')
sitecustomize.pyを設定するのは、ライブラリを使っているときに動かないものがあった
場合や、他者のコードに不備があったときなど、やむ終えない場合のみにするといいかと思います。

– 対話モード用の起動時実行ファイル

Pythonインタプリタを起動するときに読み込まれる設定ファイルを作ることができます。
とりあえず、補完の設定と、履歴の設定をしてみます。
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#-*- coding:utf-8 -*-
""" My Python Setting Files """
import os.path
import atexit

HISTORYFILE = os.path.expanduser("~/.pyhistory")

try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter

    class IrlCompleter(rlcompleter.Completer):
        """
        This class enables a "tab" insertion if there's no text for
        completion.

        The default "tab" is four spaces. You can initialize with '\t' as
        the tab if you wish to use a genuine tab.
        """
        def __init__(self, tab='    '):
            self.tab = tab
            rlcompleter.Completer.__init__(self)

        def complete(self, text, state):
            if text == '':
                readline.insert_text(self.tab)
                return None
            else:
                return rlcompleter.Completer.complete(self, text, state)

    #you could change this line to bind another key instead tab.
    readline.parse_and_bind("tab: complete")
    #readline.parse_and_bind("bind ^I rl_complete")
    readline.parse_and_bind("set input-meta on")
    readline.parse_and_bind("set convert-meta off")
    readline.parse_and_bind("set output-meta on")
    readline.set_completer(IrlCompleter().complete)
    readline.set_history_length(1000)

    # Restore our command-line history, and save it when Python exits.
    if os.path.isfile(HISTORYFILE):
        readline.read_history_file(HISTORYFILE)
    atexit.register(lambda: readline.write_history_file(HISTORYFILE))
'''
Created on 2010-09-12
@author http://stackoverflow.com/questions/
Iyori Komiyama
'''

上のファイルを作成したら .bashrc などに以下を追加します。

#PythonStartupFile
export PYTHONSTARTUP=$HOME/.pythonrc.py

これで、Pythonインタプリタで、Tab補完、履歴を使えるようになりました。

– コメント

コメントは’#’文字だけのようです。

>>> n = 234  # 変数nを定義
>>> print(n)  ####### n を表示する
234
>>> b = "abord"  ## 変数に文字列を代入
>>> print(float(n), b)  ## Python2.6.5まではまだprint文は関数になっていない
(234.0, 'abord')
>>> print(b)  ## 上はちょっと変だけど、これはok
abord
>>> n = b #これはb
>>> n
'abord'

次のように書くとエラーになるので注意

print 'a' + \ # this a
      'b' + \ # this b
      'c'     # this c

File "<input>", line 1
print 'a ' + \ #this a
               ^
SyntaxError: unexpected character after line continuation character

ドキュメンテーション

スクリプトファイルあるいは、モジュールなどの先頭、または、関数やクラスの定義直後に
「 “”” 」トリプルクウォートで囲んだ文字列を書くとドキュメンテーションになります。

例えば、randomモジュールのコードには以下のように記述されています。

def randrange(self, start, stop=None, step=1, int=int, default=None,
              maxwidth=1L<<BPF):
    """Choose a random item from range(start, stop[, step]).

    This fixes the problem with randint() which includes the
    endpoint; in Python this is usually not what you want.
    Do not supply the 'int', 'default', and 'maxwidth' arguments.
    """

これは、__doc__を使うことで読むことができます。

>>> import random
>>> print random.randrange.__doc__
Choose a random item from range(start, stop[, step]).

    This fixes the problem with randint() which includes the
    endpoint; in Python this is usually not what you want.
    Do not supply the 'int', 'default', and 'maxwidth' arguments.

もうひとつ。

def vonmisesvariate(self, mu, kappa):
    """Circular data distribution.

    mu is the mean angle, expressed in radians between 0 and 2*pi, and
    kappa is the concentration parameter, which must be greater than or
    equal to zero.  If kappa is equal to zero, this distribution reduces
    to a uniform random angle over the range 0 to 2*pi.

    """
    # mu:    mean angle (in radians between 0 and 2*pi)
    # kappa: concentration parameter kappa (>= 0)
    # if kappa = 0 generate uniform random angle

    # Based upon an algorithm published in: Fisher, N.I.,
    # "Statistical Analysis of Circular Data", Cambridge
    # University Press, 1993.

    # Thanks to Magnus Kessler for a correction to the
    # implementation of step 4.
>>> print random.vonmisesvariate.__doc__
Circular data distribution.

    mu is the mean angle, expressed in radians between 0 and 2*pi, and
    kappa is the concentration parameter, which must be greater than or
    equal to zero.  If kappa is equal to zero, this distribution reduces
    to a uniform random angle over the range 0 to 2*pi.

– 長い単一文の書き方(改行)

Pythonでは、慣例的に1行が80文字以内が推奨されています。
そのため、1文が長くなってしまうときは改行をしなければなりません。

改行には、 \ (バックスラッシュ)を用います。

>>> 1 + 2 + 3 + \
... 4 + 5
15

括弧内であれば、 \ を書かなくても改行できます。

>>> (1 + 2 + 3 +
... 4 + 5)
15
>>> [1, 2, 3, 4,
... 5, 6]
[1, 2, 3, 4, 5, 6]
>>> {1: 'a', 2: 'b',
... 3: 'c', 4: 'd'}
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

– コーディングスタイル(PEP8)

Python には、ほとんどのプロジェクトが守っているスタイルガイドとして PEP 8 があります。
それは非常に読み易く目に優しいコーディングスタイルを推奨しています。

Note

日本語訳は ふるかわとおる さんのページにあります。
  • インデントには空白 4 つを使い、タブは使わないこと。

    タブは混乱させるので、使わない方がいいらしいです。
    使っているテキストエディタの設定を変更しましょう。

  • ソースコードの幅が 79 文字を越えないように行を折り返すこと。

    80文字規制だそうです。これはなかなか難しい。

  • トップレベルの関数とクラス定義の間は、2 行空ける。クラス内部でのメソッド定義の間は 1 行空けること。

    def poge(*pon):
        """ Out the sum of all """
        return sum(pon)
    
    # 新しく書き始めるには空白を挟む
    def hoge():
        """ New define """
        print('hoge')
    
  • 可能なら、コメントはコードと同じ行に書きなさい。インラインコメントは、控えめに使う。
    コメントは、文から少なくともスペース2個分 離しておき、 # とスペース1個で始まるようにするべきである。

    value = 0   # 変数を初期化
    
  • docstring を使いなさい。

    class HogeFuga(object):
        """Do nothing."""
        pass
    
  • 演算子の前後とコンマの後には空白を入れ、括弧類のすぐ内側には空白を入れないこと: a = f(1, 2) + g(3, 4)

  • クラスや関数に一貫性のある名前を付けなさい; 慣習では CamelCase をクラス名に使い、 lower_case_with_underscores を関数名やメソッド名に使います。常に self をメソッドの第 1 引数の名前 (クラスやメソッドについては tut-firstclasses を見よ) として使いなさい。

    関数名やメソッドには小文字を、クラスは単語の先頭を大文字にする。

  • あなたのコードを世界中で使ってもらうつもりなら、風変りなエンコーディングは使わないこと。どんな場合でも ASCII が最も上手くいきます。Python3.0 以降のバージョンでは、 UTF-8 が好ましい。 PEP 3120 を参照のこと。

などなど。

— pep8を使ってみる

pep8の規則に違反していないかをチェックできるツールがあるので、試してみましょう。

$ sudo aptitude install pep8
Debian系のLinuxなら、これで PEP 8 が使えるようになります。
使っているOSでpep8が用意されていないならば、 easy_install でインストールすることができます。
$ easy_install pep8

簡単な使い方。

$ pep8 gcd.py
checking gcd.py
gcd.py:5:80: E501 line too long (145 characters)
gcd.py:15:1: E302 expected 2 blank lines, found 1

同じ規則違反があるところも表示させる。

$ pep8 -r  gcd.py
gcd.py:5:80: E501 line too long (145 characters)
gcd.py:6:80: E501 line too long (95 characters)
gcd.py:9:80: E501 line too long (82 characters)
gcd.py:15:1: E302 expected 2 blank lines, found 1
gcd.py:24:1: E302 expected 2 blank lines, found 1

Note


以上で、基本的なPythonのルールは終わりです。
もし足りないところがあった場合は、追加していきます。

(2010-09-20) sys.defaultencodeng('utf8')をsys.defaultencodeng(sys.getfilesystemencoding())に変更

0 件のコメント:

コメントを投稿