開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(クラスとオブジェクト指向プログラミング)の26章(クラスに関する高度なテクニック)の練習問題5を解いてみる。
その他参考書籍
5.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- class A: # スタティックメソッド @staticmethod def f(): # 第1引数にインスタンスを渡す必要が無い print("Hello, Static Method!") def g(self): # 通常のメソッド print("Hello, Method!") # スタティックメソッドはインスタンス無しで呼び出せる A.f() try: A.g() except Exception as err: print(type(err), err)
入出力結果(Terminal)
$ ./sample.py Hello, Static Method! <class 'TypeError'> g() missing 1 required positional argument: 'self' $
0 コメント:
コメントを投稿