The simplest way is to use the ternary operator:
a = myobject.id if myobject is not None else None
The ternary operator returns the first expression if the middle value is true, otherwise it returns the latter expression.
Note that you could also do this in another way, using exceptions:
try:
a = myobject.id
except AttributeError:
a = None
This fits the Pythonic ideal that it's easier to ask for forgiveness than permission - what is best will depend on the situation.