2012年11月3日土曜日

開発環境

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

その他参考書籍

7.

  1. エラーになる。
  2. 正しく機能しない。
  3. リスト(appendメソッドの処理対象となるオブジェクトは可変性のオブジェクトでなければならない。)keysメソッドはリストに使用できない。
  4. リストはリスト、文字列は文字列になる。

確認

対話型セッションによる入出力結果(Terminal)

$ python
Python 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> "eggs"+[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'
>>> l=[1,2,3,4,5]
>>> l
[1, 2, 3, 4, 5]
>>> l.append(6)
>>> l
[1, 2, 3, 4, 5, 6]
>>> d={'a':1,'b':2}
>>> d
{'b': 2, 'a': 1}
>>> d.append({'c':3})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' 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[1:4])
<class 'list'>
>>> "python"[1:4]
'yth'
>>> type("python"[1:4])
<class 'str'>
>>> type([1,2]+[3,4])
<class 'list'>
>>> type("python"+"eggs")
<class 'str'>
>>> quit()
$

0 コメント:

コメントを投稿