開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIV部(関数)の16章(スコープと引数)練習問題1, 2, 3, 4, 5, 6.を解いてみる。
その他参考書籍
1, 2, 3, 4, 5, 6.
入出力結果(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. >>> x = 'Spam' >>> def func(): ... print(x) ... >>> func() # 'Spam' Spam >>> def func(): ... x = 'NI' ... print(x) ... >>> func() # NI NI >>> print(x) # Spam Spam >>> def func(): ... global x ... x = 'NI' ... >>> func() # NI >>> def func(): ... x = 'NI' ... print(x) ... >>> func() # NI NI >>> print(x) # NI NI >>> x = 'Spam' >>> def func(): ... x = 'NI' ... def nested(): ... print(x) ... nested() ... >>> func() # NI NI >>> def func(a, b, c=3, d=4):print(a,b,c,d) ... >>> func(1, *(5,6)) # 1 5 6 4 1 5 6 4 >>> quit() $
ちなみにJavaScriptの場合。
コード(BBEdit)
$('#pre0').html("");
var a = b = c = d = e = "Spam",
f = function ( ) {
$('#pre0').append(a + "\n");
},
g = function( ) {
var b = 'NI';
$('#pre0').append(b + "\n");
},
h = function ( ) {
this.c = 'NI';
},
i = function ( ) {
this.d = 'NI';
var nested = function( ) {
$('#pre0').append(d + "\n");
};
nested();
},
j = function ( a,b,c,d ) {
c = c || 3,
d = d || 4;
$('#pre0').append([a,b,c,d].join(" ") + "\n");
}
f();
g();
$('#pre0').append(b + "\n");
h();
$('#pre0').append(c + "\n");
i();
$('#pre0').append(d + "\n");
j(1, 5, 6);
0 コメント:
コメントを投稿