2012年5月2日水曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(クラスとオブジェクト指向プログラミング)のまとめ演習4(Metaクラス)を解いてみる。

4.

コード(TextWrangler)

#!/usr/bin/env python
#encoding: 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 *
>>> a=Meta()
>>> a.name
get name
>>> a.name='spam'
set name spam
>>> a.name
get name
>>> a+10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Meta' and 'int'
>>> a[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Meta' object does not support indexing
>>> a+'python'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'Meta' and 'str'
>>> a[1:2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Meta' object is not subscriptable
>>> quit()
$

0 コメント:

コメントを投稿