開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のIV部(クラスとオブジェクト指向プログラミング)、まとめ演習4.(Metaクラス)を解いてみる。
その他参考書籍
まとめ演習4.(Metaクラス)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
class Meta:
def __getattr__(self, name):
print("get: {0}".format(name))
def __setattr__(self, name, value):
print("属性: {0}, set: {1}".format(name, value))
if __name__ == '__main__':
o = Meta()
o.spam
o.a = "spam"
try:
o + 10
except Exception as err:
print(type(err), err)
try:
o[0]
except Exception as err:
print(type(err), err)
try:
o[:]
except Exception as err:
print(type(err), err)
入出力結果(Terminal)
$ ./sample.py get: spam 属性: a, set: spam <class 'TypeError'> unsupported operand type(s) for +: 'Meta' and 'int' <class 'TypeError'> 'Meta' object does not support indexing <class 'TypeError'> 'Meta' object is not subscriptable $
0 コメント:
コメントを投稿