Say, I have the following Python code
if obj is None:
identifier = None
else:
identifier = obj.id
where obj is a Python object whose class definition is not included
I barely remember there's a more concise syntax (like a one-line code?) that can achieve the same. If I wasn't dreaming, can someone please advise what that simpler syntax might be? Thanks.
identifier = None if obj is None else obj.id
identifier = getattr(obj, "id", None)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With