When you have someplace where you register a set of callbacks you have a few options:
- Use an interface-style API. I.e. you have a
Mixinstyle class defining the methods expected to be present, probably just raising
NotImplementedError. You then just pass in the entire object to the
- Just try if it quacks like a duck. IMHO this is just the pythonic equivalent of the above, simply without the explicit mixin class.
- Explicitly pass in the callback functions. This would be something like:
.register(func1, ...). Secret benefit here is using the result of
functools.partial()and things like it.
What I don't like about the first two options is that you force the method name of the callback onto the user. The third option allows you to achieve the same but at the cost of a more complicated register function. But I tend to prefer it because it's more explicit and it avoids "hiding" things in the object (via the mixin) thus keeping the interfacace between caller and callee cleaner.
Are there other design considerations when making this choice? Maybe there's already an essay somewhere exploring these ideas in more detail then I'm doing here?