2013年1月1日火曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(オブジェクト指向プログラミング)26章(クラスに関する高度なテクニック)の練習問題1を解いてみる。

その他参考書籍

1.

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

# ビルトインオブジェクト、intの拡張
class MyInt(int):
    def __init__(self, data):
        self.data = data
    def __add__(self, other): # 足し算を拡張
        print("足し算!")
        res = self.data + other
        return MyInt(res)
    
a = MyInt(10)
b = MyInt(20)
c = a + b
print(c)
d = a - b
e = a * b
f = a / b
g = a // b
h = a % b
print(d, e, f, g, h)

入出力結果(Terminal)

$ ./sample.py
足し算!
30
-10 200 0.5 0 10
$

0 コメント:

コメントを投稿