Named system arguments
Currenlty, Kwant systems take positional arguments that are provided in the args
parameter to kwant.system.System.hamiltonian()
and other functions that call it.
Positional arguments are problematic when there are more than a few parameters. That's why in practice people often use an instance of SimpleNamespace
that contains named arguments as the single positional arg.
This solution is not ideal. It would be preferable to have real named system arguments. But simply accepting kwargs
in kwant.system.System.hamiltonian()
and passing them to value functions is problematic for the following reasons:
-
**kwargs
in value functions would be dictionaries; accessing their values is verbose. - There would be no way to detect and warn about unused named args.
I propose to add inspection-based named args to Kwant in the following way:
-
Add
kwargs
to low-level systems and solvers. No meaning ofkwargs
has to be imposed at this level. -
Add code to
Builder.finalized()
that walks through all value functions and inspects them. (Obviously, caching can be used to inspect each function only once.) That code will add a new instance variable to finalized builders: a dict from value functions to seqeuences of parameter names. Tentative name:_arg_names
. -
Use the above attribute in
kwant.builder.*System.hamiltonian()
to prepareargs
ifkwargs
is given:args = [kwargs[n] for n in self._arg_names[func]]
.
It should continue to be possible to write value functions like
def foo(*args):
...
We may have to introduce a decorator that allows to declare the parameters in such cases.