def safe_hasattr(obj, k):
    """Returns True if the given key exists and is not None."""
    return getattr(obj, k, None) is not None
Returns True if the given key exists and is not None.
fairseq/utils.py