開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)のまとめ演習7(オブジェクトの操作に関する質問)を解いてみる。
その他参考書籍
7.
- エラーになる。
- 正しく機能しない。
- リスト(appendメソッドの処理対象となるオブジェクトは可変性のオブジェクトでなければならない。)keysメソッドはリストに使用できない。
- リストはリスト、文字列は文字列になる。
確認
対話型セッションによる入出力結果(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 コメント:
コメントを投稿