開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド: 独自の構造を使う)、長いエクササイズ(p.228)をpythonで考えてみる。
長いエクササイズ(p.228)
コード(BBEdit, Emacs)
sample228.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
class Exercise:
def __init__(self, description, duration):
self.description = description
self.duration = duration
class Meal:
def __init__(self, ingredients, weight):
self.ingredients = ingredients
self.weight = weight
class Preferences(Meal, Exercise):
def __init__(self, ingredients, weight, description, duration):
Meal.__init__(self, ingredients, weight)
Exercise.__init__(self, description, duration)
@property
def label(self):
return '餌は{0:2.2f}キロの{1}を与え、{2}を{3:2.2f}時間行わせます。'\
.format(self.weight, self.ingredients, self.description, self.duration)
class Fish(Preferences):
def __init__(self, ingredients, weight, description, duration, name,
species, teeth, age):
Preferences.__init__(self, ingredients, weight, description, duration)
self.name = name
self.species = species
self.teeth = teeth
self.age = age
@property
def catalog(self):
return '{0}は{1}であり、歯は{2}本あります。年齢は{3}才です。'.format(
self.name, self.species, self.teeth, self.age)
if __name__ == '__main__':
snappy = Fish(name='スナッピー', species='ピラニア', teeth=69, age=4,
ingredients='肉', weight=0.1,
description='ジャクジーでの泳ぎ', duration=7.5)
print(snappy.catalog)
print(snappy.label)
入出力結果(Terminal, IPython)
$ ./sample228.py スナッピーはピラニアであり、歯は69本あります。年齢は4才です。 餌は0.10キロの肉を与え、ジャクジーでの泳ぎを7.50時間行わせます。 $
0 コメント:
コメントを投稿