開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(クラスとオブジェクト指向プログラミング)の23章(クラスのコーディング)6解いてみる。
6.
__X__(Xはinit, add, sub, mul等)というメソッドを定義すればいい。
コード(TextWrangler)
sample.py
#!/usr/bin/env python # -*- coding: utf-8 -*- class A: def __init__(self,a): self.a = a def __sub__(self,other): return self.a - other class B: def __init__(self,a): self.a = a I1 = A(10) I2 = B(10) for I in [I1,I2]: print(I - 5)
入出力結果(Terminal)
$ ./sample.py 5 Traceback (most recent call last): File "./sample.py", line 18, in <module> print(I - 5) TypeError: unsupported operand type(s) for -: 'B' and 'int' $
0 コメント:
コメントを投稿