2010-11-11から1日間の記事一覧

printの型ってなに? on Python

Ubuntu 10.10 Python 2.6 __builtins__の型を調べてたら、printだけSyntaxErrorになる なんで? print type(Exception) # <type 'type'> print type(False) # <type 'bool'> print type(open) # <type 'builtin_function_or_method'> print type(print) # SyntaxError: invalid syntax</type></type></type>

継承 on Python

Ubuntu 10.10 Python 2.6 ■プログラム(TestInheritance.py) #-------------------- # 継承 #-------------------- # 親クラスその1 class Oya_A(object): val = "AAAAA" def __init__(self): print "oya_A:init" def oyako(self): print "oya :" + self.val…

クラス on Python

Ubuntu 10.10 Python 2.6.6 ■プログラム(TestClass.py) #! /usr/bin/env python # coding:utf-8 # クラスを定義する class SampleClass(object): ''' ドキュメンテーション。クラスの説明。 ''' # インスタンス変数 moji = "aiueo" # 初期化処理 def __init_…

例外の種類を調べる

Python 2.6 # 組み込みの例外を取得する(美しくない) filter(lambda x: True if str(x).find("Error") != -1 else False, dir(__builtins__)) # PyDevで見るなら。 for err in filter(lambda x: True if str(x).find("Error") != -1 else False, dir(__bui…

例外処理 on Python

Ubuntu 10.10 Python 2.6 ■プログラム(TestError.py) #! /usr/bin/env python # coding:utf-8 class TestError(object): ''' 例外処理 ''' def __init__(self): ''' 変数初期化 ''' self.nums = [1, 2, 3, 4, 5] self.strs = ["a", "b", "c", "d", "e"] def …