Created
September 13, 2016 04:59
-
-
Save u1and0/0d08680c8cd74fef6dc0ba9ea7ac4f04 to your computer and use it in GitHub Desktop.
ディクショナリにデフォルト値ってないのか? ref: http://qiita.com/u1and0/items/e587bb26198739976281
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dic={'a':6,'b':4} | |
| # $ dic['a'] | |
| # 6 | |
| # $ dic['b'] | |
| # 4 | |
| # $ dic['c'] # キーにない値が入力されるとエラー | |
| # --------------------------------------------------------------------------- | |
| # KeyError Traceback (most recent call last) | |
| # <ipython-input-11-0c3be353eb91> in <module>() | |
| # ----> 1 ic['c'] | |
| # KeyError: 'c' | |
| $ dic.get('c',0) # dic内に'c'がないので第二引数が返される | |
| # [Out] # 0 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def func(arg1): | |
| print(arg1) | |
| # $ func() # 引数がないとエラー起こす | |
| # [Out] | |
| # --------------------------------------------------------------------------- | |
| # TypeError Traceback (most recent call last) | |
| # <ipython-input-6-08a2da4138f6> in <module>() | |
| # ----> 1 func() | |
| # TypeError: func() missing 1 required positional argument: 'arg1' | |
| def func(arg1=0): # デフォルト引数があれば引数なくても自動でデフォルト引数を使ってくれる | |
| print(arg1) | |
| # $ func() | |
| # [Out] # 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment