2012年5月29日火曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習7(オブジェクトの操作に関する質問)を解いてみる。

7.

  1. エラーが発生する。
  2. 正しく機能しない。
  3. appendメソッドはリストに使用できるメソッド。keysメソッドはリストに使用できない。
  4. 戻り値として得られるオブジェクトの型は元の型と同じ。

対話型コマンドプロンプトで確認。

入出力結果(Terminal)

$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 'spam'+[1,2,3,4,5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'list' object to str implicitly
>>> [1,2,3,4,5]+(1,2,3,4,5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
>>> {'a':1,'b':2}+{'c':3,'d':4}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>> [1,2,3,4,5].append(6)
>>> L=[1,2,3,4,5]
>>> L.append(6)
>>> L
[1, 2, 3, 4, 5, 6]
>>> S='spam'
>>> S.append('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'
>>> L.keys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'keys'
>>> type(L)
<class 'list'>
>>> M=L[1:2]
>>> type(M)
<class 'list'>
>>> N=L+M
>>> type(N)
<class 'list'>
>>> s1='spam'
>>> s2='egg'
>>> type(s1[1:2])
<class 'str'>
>>> s=s1+s2
>>> type(s)
<class 'str'>
>>> quit()
$

0 コメント:

コメントを投稿