開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(クラスとオブジェクト指向プログラミング)のまとめ演習4(Metaクラス)を解いてみる。
その他参考書籍
4.
コード(TextWrangler)
sample.py
#!/usr/bin/env python #-*- coding: utf-8 -*- class Meta: def __getattribute__(self,name): print("get:",name) def __setattr__(self,name,value): print("set:",name ,value)
入出力結果(Terminal)
$ python Python 3.2.3 (default, Apr 18 2012, 20:17:30) [GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from sample import Meta >>> meta=Meta() >>> meta.a=10 set: a 10 >>> meta.lang="python" set: lang python >>> meta.a get: a >>> meta.lang get: lang >>> meta.b get: b >>> meta+10 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Meta' and 'int' >>> meta[1] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'Meta' object does not support indexing >>> meta[:] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'Meta' object is not subscriptable >>> quit() $
メモ: 考えてた結果と違った。どこか間違えてるのか、python3.xで仕様が変わったのかどっちだろう。。
0 コメント:
コメントを投稿