開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のII部(ビルトインオブジェクト)の9章(タプル、ファイルオブジェクト、その他)練習問題6を解いてみる。
その他参考書籍
練習問題6.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
# オブジェクトがtrueであると解釈されるのは
# 数値: 0以外
# 文字列 コレクション等は空ではない時
for x in [0, 10, 0.0, 0.1, "", "python",[],[1,2],[0],(2, ), (0, )]:
print("{0}: {1}".format(x, bool(x)))
入出力結果(Terminal)
$ ./sample.py 0: False 10: True 0.0: False 0.1: True : False python: True []: False [1, 2]: True [0]: True (2,): True (0,): True $
ちなみにJavaScriptの場合。
コード(BBEdit)
var a = [0, 10, 0.0, 0.1, "", "python",[],[1,2],[0]],
result = "",
i;
for (i = 0, max = a.length; i < max; i += 1) {
result += a[i];
if(typeof(a[i]) === "object" && (a[i]).length !== "undefined"){
result += "(Array)";
}
result += ": " + Boolean(a[i]) + "\n";
}
$('#pre0').text(result);
メモ: JavaScriptで空の配列が「true」って評価されることを知った。
0 コメント:
コメントを投稿