15
PEP-3138 STRING REPRESENTATION IN PYTHON 3000 Python 3.xのオブジェクト文字列表現 Python Hack-a-thon 2010.07 Atsuo Ishimoto AXISSOFT Co., Ltd.

String representation in py3k

Embed Size (px)

DESCRIPTION

Python hackathon, Tokyo, Japan,2010

Citation preview

Page 1: String representation in py3k

PEP-3138STRING REPRESENTATION INPYTHON 3000

Python 3.xのオブジェクト文字列表現

Python Hack-a-thon 2010.07Atsuo IshimotoAXISSOFT Co., Ltd.

Page 2: String representation in py3k

オブジェクト文字列表現って?

Pythonのオブジェクトを、Pythonのスクリプトっぽい形式の文字列で表現

str()とは違う!str()は文字列オブジェクトへの型変換

>>> "abc¥tdef"

'abc¥tdef'

>>> datetime.datetime.now()

datetime.datetime(2010, 7, 9, 13, 37,

49, 107000)

>>> str(datetime.datetime.now())'2010-07-09 13:39:31.100000‘

Page 3: String representation in py3k

文字列表現の問題点

文字列オブジェクトをrepr()するとき、非ASCII文字は全部エスケープされてしまう

>>> "はろー"

‘¥x82¥xcd¥x82¥xeb¥x81[’

>>> open("はろー")

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IOError: [Errno 2] No such file or

directory: '¥x82¥xcd¥x82¥xeb¥x81[‘

Page 4: String representation in py3k

そこでPython3では!

非ASCII文字でも、エスケープしない!

空白文字・印刷不能文字は従来通りエスケープ

>>> "はろー"

'はろー'

>>> open("はろー")

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IOError: [Errno 2] No such file or

directory: 'はろー’

>>> "は¥tろ¥tー"

'は¥tろ¥tー'

Page 5: String representation in py3k

でも、楽あれば苦あり

UnicodeEncodeErrorが邪魔くさい!

環境によって、出力時にエラーが発生してしまう。デバッグには不便!

例外発生時、エラーメッセージに変換不能文字があると、本来の例外が消えてしまう

>>> "¥u0550" # ARMENIAN CAPITAL LETTER REHTraceback (most recent call last):

File "<stdin>", line 1, in <module>

UnicodeEncodeError: 'cp932' codec can't encode character

'¥u0550' in position 1:

illegal multibyte sequence

Page 6: String representation in py3k

ascii()を使う

Python 3.0で導入された組み込み関数

Python2.xと同じ文字列を生成

>>> print(ascii("hello, はろー"))

'hello, ¥u306f¥u308d¥u30fc'

Page 7: String representation in py3k

"backslashreplace"ハンドラを使う

変換できない文字は、例外を出さずに"¥uXXXX" に変換して出力する

>>> "Hello, はろー".encode("ascii")Traceback (most recent call last):

File "<stdin>", line 1, in <module>

UnicodeEncodeError: 'ascii' codec can't encode characters in position

7-9: ordinal not in range(128)

"Hello, はろー".encode("ascii",

"backslashreplace")

b'Hello, ¥u306f¥u308d¥u30fc'

Page 8: String representation in py3k

標準出力・エラー出力の使い方

sys.stderrのerrorsハンドラは'backslashreplace'

でも、sys.stdoutは’strict’

>>> print(["¥u0550"], file=sys.stderr)

['¥u0550']

>>> print(["¥u0550"], file=sys.stdout)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

UnicodeEncodeError: 'cp932' codec

can't encode character '¥u0550' in

position 2:

illegal multibyte sequence

Page 9: String representation in py3k

Errorsハンドラの設定方法

標準出力(sys.stdout)は、環境変数で設定

PYTHONIOENCODING=cp932: backslashreplace

ファイルオープン時に指定

open(filename, errors= 'backslashreplace‘)

Page 10: String representation in py3k

なんてことを言ってますが

Page 11: String representation in py3k

大きな声では言えないので

Page 12: String representation in py3k

口に出さずにスライドにだけ

書きますが

Page 13: String representation in py3k

私、Python3.x

まったく使ってません

Page 14: String representation in py3k

早く使えるようになるといいなぁ…

Page 15: String representation in py3k

……