開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART III.(Functions and Generators)、Test Your Knowledge: Part IV Exercises 、4.(Keywords)を解いてみる。
その他参考書籍
4.(Keywords)
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- def adder(good=1, bad=2, ugly=3): return good + bad + ugly for args in [(10, ), (10, 20), (10, 20, 30), (10, 20, 30, 40), ([1],), ([1], [2]), ([1], [2], [3]), ([1], [2], [3], [4])]: try: print(adder(*args)) except Exception as err: print(type(err), err, err.args) print(adder(ugly=1, good=2)) # 5 def adder1(**args): vs = list(args.values()) result = vs[0] for v in vs[1:]: result += v return result for args in [(1, 2), ([1], [2])]: try: print(adder1(*args)) except Exception as err: print(type(err), err, err.args) for args in [{'a':1, 'b':2}, {'a':[1], 'b':[2]}]: try: print(adder1(**args)) except Exception as err: print(type(err), err, err.args)
入出力結果(Terminal)
$ ./sample.py 15 33 60 <class 'TypeError'> adder() takes from 0 to 3 positional arguments but 4 were given ('adder() takes from 0 to 3 positional arguments but 4 were given',) <class 'TypeError'> can only concatenate list (not "int") to list ('can only concatenate list (not "int") to list',) <class 'TypeError'> can only concatenate list (not "int") to list ('can only concatenate list (not "int") to list',) [1, 2, 3] <class 'TypeError'> adder() takes from 0 to 3 positional arguments but 4 were given ('adder() takes from 0 to 3 positional arguments but 4 were given',) 5 <class 'TypeError'> adder1() takes 0 positional arguments but 2 were given ('adder1() takes 0 positional arguments but 2 were given',) <class 'TypeError'> adder1() takes 0 positional arguments but 2 were given ('adder1() takes 0 positional arguments but 2 were given',) 3 [1, 2] $
0 コメント:
コメントを投稿