開発環境
- 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部(クラスとクラスとオブジェクト指向プログラミング)の25章(クラスと設計)の練習問題2を解いてみる。
その他参考書籍
2.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
class A:
def __init__(self, o):
self.o = o # オブジェクトを組み込む
def __getattr__(self, attrname):
print("Trace: {0}".format(attrname))
return getattr(self.o, attrname) # 組み込まれたオブジェクトの処理を委託
a = A("python")
b = A(['a','b','c','d','e'])
c = A({'a':1,'b':2})
print(a.upper())
print(b.append(10))
print(b.o)
print(c.items())
print(c.keys())
print(c.values())
入出力結果(Terminal)
$ ./sample.py
Trace: upper
PYTHON
Trace: append
None
['a', 'b', 'c', 'd', 'e', 10]
Trace: items
dict_items([('b', 2), ('a', 1)])
Trace: keys
dict_keys(['b', 'a'])
Trace: values
dict_values([2, 1])
$
0 コメント:
コメントを投稿