简体   繁体   English

如何在 python-fire 的方法中使用默认值 arguments?

[英]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.当方法具有默认值 arguments 时,我遇到了python-fire问题。 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(sb), it is printing B123` correctly.正如评论中所见,在print(sb), it is printing了 B123`。 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.正如您在最后一个示例中看到的那样,如果我调用methodb (它有一个带有默认值的参数),它会按预期打印methoda而不是B123

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 ?我的问题是,在这种情况下,如何在不传入--x=something的情况下首先使用具有默认参数值的方法?

In short, how can i make > python x.py methodb methoda properly print B123 ?简而言之,我怎样才能使> python x.py methodb methoda正确打印B123

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.您需要将def methodb(self, x='B123'):更改为def methodb(self, *, x='B123'): ,这使x成为仅关键字参数,因此它不能再用作位置争论。 This way fire will use the default value.这样火将使用默认值。

There has also been a github issue discussing this.还有一个github issue讨论这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过google自定义命令行python-fire的默认帮助文本? - How to customize the default help text for the command line python-fire by google? 如何使用 Python 中的 class 变量作为方法的默认参数? - How can I use a class variable in Python as a default argument for a method? 如何在python中重写方法默认参数? - How are method default arguments overridden in python? 对通过python-fire触发的函数的相同参数使用多个标志 - Using multiple flags for same argument of a function fired through python-fire 如何跳过在 Python 方法中提供默认参数 - How to skip providing default arguments in a Python method 如何获取Python中任何方法的关键字/位置参数 - How can I get keyword/positional arguments of any method in Python 如何使用DELETE方法和python在pycurl中添加参数 - How can I add arguments in pycurl with DELETE method and python Python:如何在用作参数本身的函数中使用参数? - Python: How can I use arguments in a function used as argument itself? 如何设置星形参数的默认值? - How can I set a default for star arguments? 我如何在 arguments 或 python class 中使用构造函数值 - How can I either use arguments or constructor values in a python class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM