開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のIV部(関数)の17章(関数に関する高度なテクニック)練習問題5.を解いてみる。
その他参考書籍
5.
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- # 可変長の引数を使うには、通常の引数の場合は「*」キーワード引数の場合は「**」 # を使えばいい def f(a, *b, **c): for x in [a, b, c]: print(x) f(10, [1,2,3,4,5], b=1,c=2)
入出力結果(Terminal)
$ ./sample.py 10 ([1, 2, 3, 4, 5],) {'b': 1, 'c': 2} $
ちなみにJavaScriptの場合。
コード(BBEdit)
// argumentsで引数を使う function f() { var result = "", i, max; for (i = 0, max = arguments.length; i < max; i += 1) { result += arguments[i] + "\n"; } return result; } // applyとcallを利用して引数を渡す var result = f(10) + "\n" + f(1,2,3,4,5) + "\n" + f.apply(this, [10]) + "\n" + f.apply(this, [1,2,3,4,5]) + "\n" + f.call(this, 10) + "\n" + f.call(this, 1,2,3,4,5); $('#pre0').text(result);
0 コメント:
コメントを投稿