2014年7月18日金曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅱ.(Types and Operations)、Chapter 4.(Introducing Python Object Types)、Test Your Knowledge: Quiz 3.を解いてみる。

その他参考書籍

Test Your Knowledge: Quiz 3.

不変性をもつオブジェクトであるということ。(変更できない。変更したい場合は、新しいオブジェクトを作成することにより、擬似的に変更することになる。)

immutable

  1. strings
  2. numbers
  3. tuples

コード(BBEdit)

sample3.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

s = 'python'

try:
    s[0] = 'a'
except Exception as err:
    print(type(err), err)

t = (1, 2)

try:
    t[0] = 10
except Exception as err:
    print(type(err), err)

入出力結果(Terminal, IPython)

$ ./sample3.py 
<class 'TypeError'> 'str' object does not support item assignment
<class 'TypeError'> 'tuple' object does not support item assignment
$

0 コメント:

コメントを投稿