<!-- HILSTER htf 4.3.1 -->
**htf 4.3.1** · [HTML](../mixins.html) · [Complete PDF manual](../_downloads/htf-4.3.1.pdf)

# Mixins

Mixins can be used to import code without inheritance.

## Delegation

The Delegation mixins give the possibility to automatically
delegate to members of a class without calling set() or get()
directly.

Delegation is used for hardware abstractors.

### *class* htf.mixins.DelegationError

`DelegationError` is thrown if [`DelegateMixin`](#htf.mixins.DelegateMixin) is used directly without being mixed in.

### *class* htf.mixins.DelegateMixin

`DelegateMixin` defines [`set`](#htf.mixins.DelegateMixin.set) and [`get`](#htf.mixins.DelegateMixin.get) as methods that have
to be overwritten by subclass.

#### get() → [Any](https://docs.python.org/3/library/typing.html#typing.Any)

The getter for a delegate.

#### NOTE
This method is abstract.

#### set(value: [Any](https://docs.python.org/3/library/typing.html#typing.Any)) → [None](https://docs.python.org/3/builtins/constants.html#None)

The setter for a delegate.

* **Parameters:**
  **value** – the value to be set.

#### NOTE
This method is abstract.

### *class* htf.mixins.DelegateCallbackMixin(set_callback: [Callable](https://docs.python.org/3/library/typing.html#typing.Callable), get_callback: [Callable](https://docs.python.org/3/library/typing.html#typing.Callable))

`DelegateCallbackMixin` gives the possibility to user define
setter and getter methods using a [`DelegateMixin`](#htf.mixins.DelegateMixin).

* **Parameters:**
  * **set_callback** – the method to be called as a setter.
  * **get_callback** – the method to be called as a getter.

### *class* htf.mixins.DelegatorMixin

The `DelegatorMixin` automatically delegates calls for [`DelegateMixin.set`](#htf.mixins.DelegateMixin.set) and
: [`DelegateMixin.get`](#htf.mixins.DelegateMixin.get) to its members if they are instances of [`DelegateMixin`](#htf.mixins.DelegateMixin).

Example usage:

```python
class ConcreteDelegate(DelegateMixin):

    def __init__(self, parameter):
        self.parameter = parameter
        self.value = None

    def set(self, value):
        print(self.__class__.__name__ + ".set()", value)
        self.value = value

    def get(self):
        print(self.__class__.__name__ + ".get()")
        return self.value

class AnotherDelegate(ConcreteDelegate):
    pass

class A(DelegatorMixin):

    def __init__(self):
        self.member = ConcreteDelegate("parameter for member")
        self.test = AnotherDelegate("blub")

class NormalMember:

    def __init__(self, parameter):
        self.parameter = parameter
        self.value = None

    def set(self, value):
        print(self.__class__.__name__ + ".set()", value)
        self.value = value

    def get(self):
        print(self.__class__.__name__ + ".get()")
        return self.value

a = A()
print(a.member)
a.member = 1
print(a.member)
a.b = 2
print(a.b)
a.test = 1337
print(a.test)
a.blub = AnotherDelegate("rolf") # a new delegate is instantiated
a.blub = "peter" # now setting a value leads to a delegated call to AnotherDelegate().set("peter")
print(a.blub) # new reading is delegated to it
a.normalMember = NormalMember("normal")
a.normalMember = "test"
print(a.normalMember)
```
