開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Functions and Generators)、Test Your Knowledge: Part IV Exercises 、5.(Dictionary tools)を解いてみる。
その他参考書籍
5.(Dictionary tools)
コード(BBEdit)
sample.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def copyDict(dict):
result = {}
for key, value in dict.items():
result[key] = value
return result
dict = {'a':1, 'b':2}
new_dict = copyDict(dict)
l = [dict, new_dict]
for d in l:
print(d)
dict['a'] = 10
for d in l:
print(d)
try:
new_dict = dict[:]
print(new_dict)
except Exception as err:
print(type(err), err, err.args)
入出力結果(Terminal)
$ ./sample.py
{'b': 2, 'a': 1}
{'b': 2, 'a': 1}
{'b': 2, 'a': 10}
{'b': 2, 'a': 1}
<class 'TypeError'> unhashable type: 'slice' ("unhashable type: 'slice'",)
$
0 コメント:
コメントを投稿