This question already has an answer here:
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(myobject.id, None)