Callables¶
Tip
Code for examples on this page can be found in examples/18_callables.
Callables are accepted as leaf types. They deserve their own tutorial as they are a bit more involved than the average leaf type.
Take this configuration, which holds a Callable[[str], None] function:
This callable is used to display a greeting message like so:
The callable can be specified in the following ways.
Functions¶
The callable can be set to a function by specifying its fully-qualified name (FQN).
An expanded dict form can also be used, where the FQN of the function is given under the bind key (what it binds is the subject of Tutorial #19).
Classes¶
To use an instance of a class as a callable, we must use an expanded dict declaration, giving the FQN of the class under the usual class key.
Additional keys needed to instantiate the class are given alongside the class key, as usual.
$ uv run print_greetings.py --greet_fn.class greetings.Print_greetings --greet_fn.greetings Hi
Hi, world!
Note
In case you wonder why we explicitly use the class key here to specify the class FQN as such, we will see in Tutorial #20 what happens when we use the class as a callable.
Class methods¶
Like functions, class methods can be specified as callables. In the simplest case, when the object can be instantiated without arguments, the FQN of the method is passed either directly under the field key or under the bind key of an expanded dict definition.
$ # 1. Directly under the key
$ uv run print_greetings.py --greet_fn greetings.Greetings_printer.print
Hello, world!
$ # 2. In an expanded dict definition under the `bind` key
$ uv run print_greetings.py --greet_fn.bind greetings.Greetings_printer.print
Hello, world!
When parameters are needed to initialize the object, the FQN of the method goes under the classfn key instead, and the parameters alongside it are given to the object's constructor.