開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅱ.(Types and Operations)、Chapter 9.(Tuples, Files, and Everything Else)、Test Your Knowledge: Quiz 2.を解いてみる。
その他参考書籍
Test Your Knowledge: Quiz 2.
コード(BBEdit)
sample2.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
# topuleはimmutable(不変性を持つオブジェクト)なので、既存のtupleを変更したい場合は、新しいtupleを作成することになる
t = (4, 5, 6)
t = (1,) + t[1:]
print(t)
# 以下はエラーになる
try:
t[0] = 100
print(t)
except Exception as err:
print(type(err), err)
入出力結果(Terminal, IPython)
$ ./sample2.py (1, 5, 6) <class 'TypeError'> 'tuple' object does not support item assignment $
0 コメント:
コメントを投稿