2013年3月4日月曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIV部(関数)のまとめ演習1.(簡単な関数)を解いてみる。

その他参考書籍

1.(簡単な関数)

入出力結果(Terminal)

$ python
Python 3.3.0 (default, Sep 29 2012, 08:16:08) 
[GCC 4.2.1 Compatible Apple Clang 3.1 (tags/Apple/clang-318.0.58)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> def f(x):
...     print(x)
... 
>>> f("spam")
spam
>>> f(10)
10
>>> f([1,2,3,4,5])
[1, 2, 3, 4, 5]
>>> f({'a':1,'b':2})
{'b': 2, 'a': 1}
>>> f((1,2,3,4,5))
(1, 2, 3, 4, 5)
>>> f({'a','b',1,2})
{1, 2, 'a', 'b'}
>>> f() # エラーになる
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'x'
>>> f(1,2) # エラーになる
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() takes 1 positional argument but 2 were given
>>> quit()
$

ちなみにJavaScriptの場合。

コード(BBEdit)

$('#pre0').text('');
function f( a ) {
    $('#pre0').append(a);
}
var a = ["JavaScript", 10, [1,2,3,4,5], {'a':1,'b':2},true],
    i, max;
for (i = 0, max = a.length; i < max; i += 1) {
    f(a[i]);
    $('#pre0').append("\n");
}
// Pythonと違いJavaScriptでは、引数が少ないとundefined、多すぎても残りが
// 無視されるだけ(関数内でargumentsとか使えば利用する事も出来る
f();
$('#pre0').append("\n");
f(1,2);










						

0 コメント:

コメントを投稿