I interpret "if there is no human object or the human object does not have a birth date" means that if the result object does not have the "person" attribute, and also result.person object doesn 't have the' birthdate 'attribute. However, I notice that your previous question uses strange terminology, such as "person object set to None" (in the comment). How to set an object in None? Do you mean the person attribute set to None? When you ask a question, (1) please make it standalone and (2) fully explain what your actual data structure is.
import datetime large_date = datetime.datetime(9999, 12, 31) results.sort(key=lambda result: result.person.birthdate if hasattr(result, 'person') and hasattr(result.person, 'birthdate') else large_date )
John machin
source share