2012年12月24日月曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(オブジェクト指向プログラミング)24章(クラスのコーディング(詳細))の練習問題2を解いてみる。

その他参考書籍

2.

コード(BBEdit)

sample.py

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

class Cubes:
    def __init__(self, stop):
        self.stop = stop
        self.data = -1

    def __getitem__(self,index):
        if index < 0 or self.stop <= index:
            raise IndexError
        return index ** 3

cubes = Cubes(10)

for x in cubes:
    print(x)

入出力結果(Terminal)

$ ./sample.py
0
1
8
27
64
125
216
343
512
729
$

0 コメント:

コメントを投稿