Binding arguments¶
Tip
Code for examples on this page can be found in examples/19_bindings.
When specifying a callable, we may want to bind some of its parameters to fixed values. This is done with the dedicated bind key: it names the callable, and every other key beside it is an argument bound to it.
To functions¶
To bind a value to a function argument, we use the expanded dict definition, where the FQN of the function is given under the bind key and the arguments alongside it.
$ uv run print_greetings.py --greet_fn.bind greetings.print_greetings --greet_fn.greetings Hi
Hi, world!
In configuration files, this translates as — a function used as-is, in the short form:
Note
If the function has an argument named bind (or class, or call) that would conflict with the directive, confarg accepts an alternative set of directives prefixed with _:
$ uv run print_greetings.py --greet_fn._bind greetings.print_greetings --greet_fn.greetings Hi
Hi, world!
In that case, the bind argument is spelled plainly, and all directives of that spec must be drawn from the alternative set.
To callable classes¶
To bind arguments to a callable class, bind names the instance to bind them to: a nested spec, where the FQN of the class is given under the usual class key. The arguments of the call sit beside bind, as for a function.
$ uv run print_greetings.py --greet_fn.bind.class greetings.Print_greetings --greet_fn.adjective beautiful
Hello, beautiful world!
If the class needs initialization parameters, simply add them alongside the class key, inside the nested spec.
$ uv run print_greetings.py --greet_fn.bind.class greetings.Print_greetings --greet_fn.bind.greetings Hi --greet_fn.adjective beautiful
Hi, beautiful world!
In configuration files, this translates as — a callable class used as-is:
Note
If the class has a bind argument in its __init__ method that would conflict with the directive, the nested spec can use the alternative set of directives prefixed with _:
$ uv run print_greetings.py --greet_fn.bind._class greetings.Print_greetings --greet_fn.bind.greetings Hi --greet_fn.adjective beautiful
Hi, beautiful world!
Each spec picks its own set: here only the nested class spec, whose keys are the constructor's, is spelled with _.
To class methods¶
When binding arguments to a class method, the FQN of the method is given under the bind key. The object is created without arguments, and the arguments beside bind are bound to the method.
$ uv run print_greetings.py --greet_fn.bind greetings.Greetings_printer.print --greet_fn.adjective beautiful
Hello, beautiful world!
If the object needs initialization parameters, bind names a nested spec instead, where the FQN of the method is given under the classfn key and the initialization parameters alongside it. The arguments beside bind are still bound to the method.
$ uv run print_greetings.py --greet_fn.bind.classfn greetings.Greetings_printer.print --greet_fn.bind.greetings Hi --greet_fn.adjective beautiful
Hi, beautiful world!
In configuration files, this translates as — an object method used as-is, in the short form: