I find myself somewhat often wanting default values that depend on other attributes of the object, e.g. the default output path is a function of the input path, etc. The way to do this currently is to use the get argument:
appcli.param(
'--output',
default=None,
get=lambda self, x: x or self.path.with_suffix('.out'),
)
However, this is less-than-ideal for two reasons:
It's not intuitive: the default is just there to makes things work, it's not a true default.
Get functions aren't cached, and in some cases caching these defaults would be desireable.
Possible solutions:
Add another kind of default argument, e.g. default_self, that is like default_factory but is passed the object itself as an argument:
I'm not crazy about the name, though. Some alternatives:
default_self
default_method: more likely to be a lambda than a true method, but it does kinda convey the idea.
Inspect default_factory, and pass it the object if it takes a single positional argument. I don't really like this; it just seems to hacky and fragile. I can imagine scenarios with default arguments that would give surprising results.
I find myself somewhat often wanting default values that depend on other attributes of the object, e.g. the default output path is a function of the input path, etc. The way to do this currently is to use the
get
argument:However, this is less-than-ideal for two reasons:
Possible solutions:
Add another kind of default argument, e.g.
default_self
, that is likedefault_factory
but is passed the object itself as an argument:I'm not crazy about the name, though. Some alternatives:
default_self
default_method
: more likely to be a lambda than a true method, but it does kinda convey the idea.Inspect
default_factory
, and pass it the object if it takes a single positional argument. I don't really like this; it just seems to hacky and fragile. I can imagine scenarios with default arguments that would give surprising results.