def copy_attributes(source, target, attribute_names, overwrite=True):
    """
    Copies attributes from source object to target object, overwriting if already exists.

    :param source: the source object (dataset, variable, etc)
    :param target: the target object
    :param attribute_names: tuple / list of attribute names to copy across
    """
    for attribute_name in attribute_names:
        if attribute_name in '_FillValue':
            continue
        if hasattr(target, attribute_name) and not overwrite:
            raise Exception(
                'Attribute already exists in target, but overwrite is false')
        setattr(target, attribute_name, getattr(source, attribute_name))
