2012年4月10日火曜日

開発環境

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

7.

コード(TextWrangler)

#!/usr/bin/env python
#encoding: 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 1 4 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 2.7.2 (default, Feb 12 2012, 23:50:38) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sample import *
>>> f1(1,2) # 1 2
1 2
>>> f1(b=1,a=2) # 2 1
2 1
>>> f2(1,2,3) # 1 (2,3)
1 (2, 3)
>>> f3(1,x=2,y=3) # 1 {'x':2,'y':3}
1 {'y': 3, 'x': 2}
>>> f4(1,2,3,x=2,y=3) # 1 (2,3) {'x':2,'y':3}
1 (2, 3) {'y': 3, 'x': 2}
>>> 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,)
>>> f6(1,3,4) # 1 3 (4,)
1 3 (4,)
>>> quit()
$

0 コメント:

コメントを投稿