2014年5月9日金曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅵ.(Classes and OOP)、CHAPTER 32(Advanced Class Topics)、Test Your Knowledge: Part VI Exercises 6.(Class tree links)を解いてみる。

その他参考書籍

6.(Class tree links)

コード(BBEdit)

lister.py

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

class ListInstance:
    def __attrnames(self):
        result = ''
        for attr in sorted(self.__dict__):
            result += '\t{0}={1}\n'.format(attr, self.__dict__[attr])
        return result

    def __str__(self):
        return '<Instance of {0}({1}), address {2}:\n{3}>'.format(
            self.__class__.__name__,
            self.__supers(),
            id(self),
            self.__attrnames())
    def __supers(self):
        names = []
        return ', '.join(cls.__name__ for cls in self.__class__.__bases__)

class Super:
    def __init__(self):
        self.data1 = 'spam'
    def ham(self):
        pass

class Sub(Super, ListInstance):
    def __init__(self):
        Super.__init__(self)
        self.data2 = 'eggs'
        self.data3 = 42
    def spam(self):
        pass

if __name__ == '__main__':
    x = Sub()
    print(x)

入出力結果(Terminal)

$ ./lister.py 
<Instance of Sub(Super, ListInstance), address 4332802120:
 data1=spam
 data2=eggs
 data3=42
>
$

0 コメント:

コメントを投稿