default#

ivy.default(x, /, default_val, *, catch_exceptions=False, rev=False, with_callable=False)[source]#

Return x provided it exists (is not None), else returns default value.

Parameters:
  • x (Any) – Input which may or may not exist (be None).

  • default_val (Any) – The default value.

  • catch_exceptions (bool, default: False) – Whether to catch exceptions from callable x. Default is False.

  • rev (bool, default: False) – Whether to reverse the input x and default_val. Default is False.

  • with_callable (bool, default: False) – Whether either of the arguments might be callable functions. Default is False.

Return type:

Any

Returns:

ret – x if x exists (is not None), else default.

Examples

With Any input:

>>> x = None
>>> y = ivy.default(x, "default_string")
>>> print(y)
default_string
>>> x = ""
>>> y = ivy.default(x, "default_string")
>>> print(y)
>>> x = ivy.array([4, 5, 6])
>>> y = ivy.default(x, ivy.array([1, 2, 3]), rev=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda: ivy.array([1, 2, 3])
>>> y = ivy.default(x, ivy.array([4, 5, 6]), with_callable=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda: None
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), with_callable=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda: None
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), catch_exceptions=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda a, b: a + b
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), with_callable=True,
...                 catch_exceptions=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda a, b: a + b
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), with_callable=True,
...                 catch_exceptions=True, rev=True)
>>> print(y)
ivy.array([1, 2, 3])
Array.default(self, /, default_val, *, catch_exceptions=False, rev=False, with_callable=False)[source]#

ivy.Array instance method variant of ivy.default. This method simply wraps the function, and so the docstring for ivy.default also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array

  • default_val (Any) – The default value.

  • catch_exceptions (bool, default: False) – Whether to catch exceptions from callable x. Default is False.

  • rev (bool, default: False) – Whether to reverse the input x and default_val. Default is False.

  • with_callable (bool, default: False) – Whether either of the arguments might be callable functions. Default is False.

Return type:

Any

Returns:

ret – x if x exists (is not None), else default.

Examples

>>> x = ivy.array([1, 2, 3, 1.2])
>>> y = x.default(0)
>>> print(y)
ivy.array([1. , 2. , 3. , 1.2])