2012年8月20日月曜日

開発環境

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

1.

クラス他の変数と同様にいずれかのモジュールに属する。

コード(TextWrangler)

sample.py

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

class sample:
 def __init__(self):
  self.a = "Hello, Class!"
 def f():
  print("Hello, Python!")
 def g(self):
  print(self.a)

入出力結果(Terminal)

$ python
Python 3.2.3 (default, Apr 18 2012, 20:17:30) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sample
>>> sample.sample.f()
Hello, Python!
>>> I = sample.sample()
>>> I.g()
Hello, Class!
>>> from sample import sample
>>> sample.f()
Hello, Python!
>>> I=sample()
>>> I.g()
Hello, Class!
>>> quit()
$

0 コメント:

コメントを投稿