'How can I use default arguments in a method with python-fire?
I am having an issue with python-fire when a method has arguments with default values. Consider the following code:
import fire
class SomeClass(object):
def __init__(self):
self.a = ''
self.b = ''
def methoda(self):
self.a = 'A'
return self
def methodb(self, x='B123'):
self.b = self.a + x
return self
def __str__(self):
return self.b
if __name__ == '__main__':
s = SomeClass()
s.methodb().methoda()
print(s.b) # this prints B123 correctly
fire.Fire(SomeClass)
As seen in the comments, in the print(s.b), it is printingB123` correctly. But when i change the order of the methods being called in fire from the command line, I am getting the odd behavior.
Example:
> python x.py
B123 # correct
> python x.py methoda methodb
Here: B123
AB123 # both correct
> python x.py methodb --x B123 methoda
Here: B123
B123 # again both correct
> python x.py methodb methoda
Here: B123
methoda # this is not correct. It should print B123 here also
As you can see with the last example, if i call methodb (which has an argument with a default value), it prints methoda instead of B123 as expected.
My question is, How can I use a method that has a default argument value first in this type of scenario without passing in --x=something?
In short, how can i make > python x.py methodb methoda properly print B123?
Solution 1:[1]
You need to change def methodb(self, x='B123'): to def methodb(self, *, x='B123'):, which makes x a keyword only argument, so it can no longer be used as a positional argument. This way fire will use the default value.
There has also been a github issue discussing this.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | M. Gruber |
