hasattr is a method used for checking the existence of an attribute in an object.
Example -
if hasattr(objRef, 'propName'):
objRef.propName
You can simply use the code below to check the existence of attribute 'displayname' and if it doesn't exists then use value of name as there is always a name attribute with this field you have mentioned in the question.
display_name = worklog.author.displayName if hasattr(worklog.author, "displayName") else worklog.author.name