How can I return a default value for an attribute? [duplicate]
问题 This question already has an answer here: A get() like method for checking for Python attributes 3 answers I have an object myobject , which might return None . If it returns None , it won't return an attribute id : a = myobject.id So when myobject is None , the stament above results in a AttributeError : AttributeError: 'NoneType' object has no attribute 'id' If myobject is None, then I want a to be equal to None . How do I avoid this exception in one line statement, such as: a = default