Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 14(Object-Oriented Programming)、14.8(Exercises) 4.を解いてみる。
14.8(Exercises) 4.
コード(BBEdit)
sample4.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Nematode:
def __init__(self, body_length, gender, age):
self.body_length = body_length
self.gender = gender
self.age = age
def __repr__(self):
return 'Nematode(body_length = {0}, gender={1}, age={2})'.format(
self.body_length, self.gender, self.age)
def __str__(self):
return 'Number\nbody length: {0}\ngender: {1}\nage:{2}'.format(
self.body_length, self.gender, self.age)
if __name__ == '__main__':
n1 = Nematode(1, 'hermaphrodite', 10)
n2 = Nematode(1, 'male', 20)
for nematode in (n1, n2):
print(repr(nematode))
print(nematode)
入出力結果(Terminal, IPython)
$ ./sample4.py Nematode(body_length = 1, gender=hermaphrodite, age=10) Number body length: 1 gender: hermaphrodite age:10 Nematode(body_length = 1, gender=male, age=20) Number body length: 1 gender: male age:20 $
0 コメント:
コメントを投稿