Skip to content

Choice

Bases: Collection[T]

A list of values to choose from in a hyperparameter search.

Source code in src/safeds/ml/hyperparameters/_choice.py
class Choice(Collection[T]):
    """A list of values to choose from in a hyperparameter search."""

    def __init__(self, *args: T) -> None:
        """
        Create a new choice.

        Parameters
        ----------
        *args: tuple[T, ...]
            The values to choose from.
        """
        self.elements = list(args)

    def __contains__(self, value: Any) -> bool:
        """
        Check if a value is in this choice.

        Parameters
        ----------
        value: Any
            The value to check.

        Returns
        -------
        is_in_choice : bool
            Whether the value is in this choice.
        """
        return value in self.elements

    def __iter__(self) -> Iterator[T]:
        """
        Iterate over the values of this choice.

        Returns
        -------
        iterator : Iterator[T]
            An iterator over the values of this choice.
        """
        return iter(self.elements)

    def __len__(self) -> int:
        """
        Get the number of values in this choice.

        Returns
        -------
        number_of_values : int
            The number of values in this choice.
        """
        return len(self.elements)

elements = list(args) instance-attribute

__contains__(value)

Check if a value is in this choice.

Parameters:

Name Type Description Default
value Any

The value to check.

required

Returns:

Name Type Description
is_in_choice bool

Whether the value is in this choice.

Source code in src/safeds/ml/hyperparameters/_choice.py
def __contains__(self, value: Any) -> bool:
    """
    Check if a value is in this choice.

    Parameters
    ----------
    value: Any
        The value to check.

    Returns
    -------
    is_in_choice : bool
        Whether the value is in this choice.
    """
    return value in self.elements

__init__(*args)

Create a new choice.

Parameters:

Name Type Description Default
*args T

The values to choose from.

()
Source code in src/safeds/ml/hyperparameters/_choice.py
def __init__(self, *args: T) -> None:
    """
    Create a new choice.

    Parameters
    ----------
    *args: tuple[T, ...]
        The values to choose from.
    """
    self.elements = list(args)

__iter__()

Iterate over the values of this choice.

Returns:

Name Type Description
iterator Iterator[T]

An iterator over the values of this choice.

Source code in src/safeds/ml/hyperparameters/_choice.py
def __iter__(self) -> Iterator[T]:
    """
    Iterate over the values of this choice.

    Returns
    -------
    iterator : Iterator[T]
        An iterator over the values of this choice.
    """
    return iter(self.elements)

__len__()

Get the number of values in this choice.

Returns:

Name Type Description
number_of_values int

The number of values in this choice.

Source code in src/safeds/ml/hyperparameters/_choice.py
def __len__(self) -> int:
    """
    Get the number of values in this choice.

    Returns
    -------
    number_of_values : int
        The number of values in this choice.
    """
    return len(self.elements)