2013年3月10日日曜日

開発環境

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

その他参考書籍

7.(引数のバリエーション)

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

def f1(a, b): print(a, b)

def f2(a, *b): print(a, b)

def f3(a, **b): print(a, b)

def f4(a, *b, **c):print(a, b, c)

def f5(a, b=2, c=3): print(a, b, c)

def f6(a, b=2, *c): print(a, b, c)

入出力結果(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.
>>> from sample import *
>>> f1(1, 2) # 1 2
1 2
>>> f1(b=2, a=1) # 1 2
1 2
>>> f2(1, 2, 3) # 1 (2, 3)
1 (2, 3)
>>> f3(1, x=2, y=3) # 1 {'x':2,'y':3}
1 {'x': 2, 'y': 3}
>>> f4(1,2,3,x=2, y=3) # 1 (2, 3) {'x':2,'y':3}
1 (2, 3) {'x': 2, 'y': 3}
>>> f5(1) # 1 2 3
1 2 3
>>> f5(1, 4) # 1 4 3
1 4 3
>>> f6(1) # 1 2 ()
1 2 ()
>>> f6(1, 3, 4) # 1 3 (4,)
1 3 (4,)
>>> quit()
$

ちなみにJavaScriptの場合。

コード(BBEdit)

$('#pre0').text("");
var f1 = function ( a, b ) {
    $('#pre0').append(a, b, "\n");
},
    f2 = function ( a, b, c ) {
        b = b || 2,
        c = c || 3;
        $('#pre0').append(a, b, c, "\n");
    }

f1(1, 2);
f2(1);
f2(1, 4);









						

0 コメント:

コメントを投稿