MultiMethod

class sunpy.util.multimethod.MultiMethod(get)[source] [edit on github]

Bases: object

A multimethod is a callable object that decides which code to execute based on the type of one or more of its arguments.

Parameters:get (function) – function which receives args and kwargs and returns a tuple of values to consider for dispatch.

Methods Summary

__call__(*args, **kwargs) Call self as a function.
add(fun, types[, override]) Add fun to the multimethod.
add_dec(*types, **kwargs) Return a decorator that adds the function it receives to the multimethod with the types passed as *args.
super(*args, **kwargs) Like __call__, only that when you give it super(cls, obj) items, it will skip the multimethod for cls and use the one for its parent class.

Methods Documentation

__call__(*args, **kwargs)[source] [edit on github]

Call self as a function.

add(fun, types, override=0)[source] [edit on github]

Add fun to the multimethod. It will be executed if get returns values of the types passed as types. Must return tuples of same length for any input.

Parameters:
  • fun (function) – function to be added to the multimethod
  • types (tuple of classes) – types for which the function is executed
  • override (SILENT, WARN or FAIL) – control behaviour when overriding existing definitions. If it is set to SILENT, prior definitions are silently overridden, if it is set to WARN a TypeWarning will be issued, and with FAIL a TypeError is raised when attempting to override an existing definition.
add_dec(*types, **kwargs)[source] [edit on github]

Return a decorator that adds the function it receives to the multimethod with the types passed as *args. Using keyword arg override to control overriding behaviour. Compare add.

super(*args, **kwargs)[source] [edit on github]

Like __call__, only that when you give it super(cls, obj) items, it will skip the multimethod for cls and use the one for its parent class. The normal __call__ does not consider this for performance reasons.