開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語: 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 __getattribute__(self, name): print("get {0}".format(name)) def __setattr__(self, name, value): print("set {0} {1}".format(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. >>> import sample >>> meta = sample.Meta() >>> meta.name get name >>> meta.a = "python" set a python >>> 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はPython2.xとは若干フックメソッドの挙動が変わってるみたい。
0 コメント:
コメントを投稿