sklearn.utils.metaestimators.available_if

sklearn.utils.metaestimators.available_if(check)[source]

An attribute that is available only if check returns a truthy value

Parameters
checkcallable

When passed the object with the decorated method, this should return a truthy value if the attribute is available, and either return False or raise an AttributeError if not available.

Examples

>>> from sklearn.utils.metaestimators import available_if
>>> class HelloIfEven:
...    def __init__(self, x):
...        self.x = x
...
...    def _x_is_even(self):
...        return self.x % 2 == 0
...
...    @available_if(_x_is_even)
...    def say_hello(self):
...        print("Hello")
...
>>> obj = HelloIfEven(1)
>>> hasattr(obj, "say_hello")
False
>>> obj.x = 2
>>> hasattr(obj, "say_hello")
True
>>> obj.say_hello()
Hello

Examples using sklearn.utils.metaestimators.available_if