def safe_getattr(obj, k, default=None):
    """Returns obj[k] if it exists and is not None, otherwise returns default."""
    from omegaconf import OmegaConf

    if OmegaConf.is_config(obj):
        return obj[k] if k in obj and obj[k] is not None else default

    return getattr(obj, k, default)
Returns obj[k] if it exists and is not None, otherwise returns default.
structured_prompting/fairseq-version/fairseq/fairseq/utils.py