開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(クラスとオブジェクト指向プログラミング)のまとめ演習4.(Metaクラス)を解いてみる。
その他参考書籍
4.(Metaクラス)
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- class Meta: def __getattr__(self, name): print("get:",name) def __setattr__(self, name, value): print("set:", name, value)
入出力結果(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. >>> from sample import * >>> m = Meta() >>> m.a get: a >>> m.b="python" set: b python >>> m + 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Meta' and 'int' >>> m[1] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'Meta' object does not support indexing >>> m[:] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'Meta' object is not subscriptable >>> quit() $
0 コメント:
コメントを投稿