開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅵ.(Classes and OOP)、CHAPTER 32(Advanced Class Topics)、Test Your Knowledge: Part VI Exercises 4.(Attribute methods)を解いてみる。
その他参考書籍
4.(Attribute methods)
コード(BBEdit)
sample.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
class Attrs:
def __getattr__(self, name):
print(name)
def __setattr__(self, name, value):
print(name, value)
if __name__ == '__main__':
def p(err):
print(type(err), err, err.args)
a = Attrs()
a.attr1 = 10
a.attr1
a.attr2 = [1,2]
a.attr2
b = Attrs()
try:
a + b
except Exception as err:
p(err)
try:
a + 1
except Exception as err:
p(err)
try:
a[0]
except Exception as err:
p(err)
try:
a[:]
except Exception as err:
print(err)
入出力結果(Terminal)
$ ./sample.py
attr1 10
attr1
attr2 [1, 2]
attr2
<class 'TypeError'> unsupported operand type(s) for +: 'Attrs' and 'Attrs' ("unsupported operand type(s) for +: 'Attrs' and 'Attrs'",)
<class 'TypeError'> unsupported operand type(s) for +: 'Attrs' and 'int' ("unsupported operand type(s) for +: 'Attrs' and 'int'",)
<class 'TypeError'> 'Attrs' object does not support indexing ("'Attrs' object does not support indexing",)
'Attrs' object is not subscriptable
$
0 コメント:
コメントを投稿