Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Python dictの場合、次のことができます
a = {'b': 5} c = a.get('c', 0)
キーが存在しない場合のデフォルト値を持つため。Python インスタンス変数に同様の「デフォルト値」機能はありますか?
組み込み関数はオプションのgetattr3 番目の引数を取り、オブジェクトが属性を持たない場合に返されます。使用例:getattr(myobj, 'attr', default_value)
getattr
getattr(myobj, 'attr', default_value)