def has_attribute(trait, attribute_name, attribute_value=None,
                  recursive=True):
    """ Checks if a given trait has an attribute and optionally if it
    is set to particular value.

    Parameters
    ----------
    trait: Trait
        the input trait object.
    attribute_name: string
        the trait attribute name that will be checked.
    attribute_value: object (optional)
        the trait attribute axpected value.
    recursive: bool (optional, default True)
        check for the attribute in the inner traits.

    Returns
    -------
    res: bool
        True if input given trait has an attribute and optionally if it
        is set to a particular value.
    """
    # Count the nomber of trait having the target attribute
    count = 0

    # Check the current trait
    if (attribute_name in trait.__dict__ and
        (trait.__dict__[attribute_name] == attribute_value or
         attribute_value is None)):
            count += 1

    # Check the inner traits
    if recursive:
        if len(trait.inner_traits) > 0:
            for inner_trait in trait.inner_traits:
                count += has_attribute(inner_trait, attribute_name,
                                       attribute_value, recursive)

    return count > 0