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 dicts,可以做
a = {'b': 5} c = a.get('c', 0)
为了在密钥不存在的情况下具有默认值。Python 实例变量是否有类似的“默认值”功能?
getattr内置函数采用可选的第三个参数,如果对象没有该属性,则返回该参数。示例用法:getattr(myobj, 'attr', default_value)
getattr
getattr(myobj, 'attr', default_value)