2013年1月2日水曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(オブジェクト指向プログラミング)26章(クラスに関する高度なテクニック)の練習問題2、5を解いてみる。

その他参考書籍

2, 5.

コード(BBEdit)

sample.py

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

class A:
    @staticmethod # 関数デコレータ
    def g(): # スタティックメソッド(インスタンス無しで呼び出せる)
        print("Hello, Staticmethod!")
    def f(self): # 通常のメソッド
        print("Hello, Method!")

A.g()
try:
    A.f()
except Exception as err:
    print(err)

a = A()
a.g()
a.f()

入出力結果(Terminal)

$ ./sample.py
Hello, Staticmethod!
f() missing 1 required positional argument: 'self'
Hello, Staticmethod!
Hello, Method!
$

ちなみにJavaScriptの場合。

コード(BBEdit)


var Obj = function(){};
Obj.prototype.f = function(){
  $('#pre0').append("Hello, Method!\n");
};
Obj.g = function(){
  $('#pre0').append("Hello, Staticmethod\n");
};
Obj.g();
try{
  Obj.f();
} catch(e){
  $('#pre0').append(e + "\n");
}
var o = new Obj();
try{
o.g();
}catch (e){
  $('#pre0').append(e + "\n");
}
o.f();









						

0 コメント:

コメントを投稿