argparse:如何调用方法而不是函数?

发布于 2024-10-11 01:15:51 字数 333 浏览 0 评论 0原文

我在 python 程序中使用 argparse,当我运行我的软件时,我想调用一个方法,如下所示:

$ python __init__.py foo

如果我想调用一个函数而不是一个方法,这很容易:

def foo(args):
    pass

def main():
    foo_parser.set_defaults(func=foo)

if __name__ == "__main__":
    main()

我怎样才能替换“ func=foo”通过“func=MyClass.my_method”?

I am using argparse in my python program and I want to call a method when I run my software like this :

$ python __init__.py foo

It is easy if I want to call a function instead of a method :

def foo(args):
    pass

def main():
    foo_parser.set_defaults(func=foo)

if __name__ == "__main__":
    main()

How can I replace "func=foo" by "func=MyClass.my_method" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与风相奔跑 2024-10-18 01:15:51

您只需引用方法而不是函数。是的。就是这么简单。而不是

 func = foo

You do

 func = theobject.foo

Done!

You just refer to the method instead of the function. Yup. It's that easy. Instead of

 func = foo

You do

 func = theobject.foo

Done!

一曲琵琶半遮面シ 2024-10-18 01:15:51

你自己给出了正确答案。只需输入:

func = Your_Class.your_method

而不是:

func = foo

You gave the correct answer yourself. Just put:

func = Your_Class.your_method

instead of:

func = foo
揽月 2024-10-18 01:15:51

您是否正在寻找这样的东西:

import argparse

class Myclass(object):

    def foo(self):
        print 'foo'

    def bar(self):
        print 'bar'

class Main(Myclass):

    def __init__(self):
        foo_parser = argparse.ArgumentParser()

        foo_parser.add_argument('method')
        self.args = foo_parser.parse_args()

    def __call__(self, *args, **kws):
        method = self.args.method
        return getattr(self, method)(*args, **kws)

if __name__ == "__main__":
    main = Main()

    main()

用法示例:

$ python test.py foo
'foo'
$ python test.py bar
'bar'

Are you looking for something like this :

import argparse

class Myclass(object):

    def foo(self):
        print 'foo'

    def bar(self):
        print 'bar'

class Main(Myclass):

    def __init__(self):
        foo_parser = argparse.ArgumentParser()

        foo_parser.add_argument('method')
        self.args = foo_parser.parse_args()

    def __call__(self, *args, **kws):
        method = self.args.method
        return getattr(self, method)(*args, **kws)

if __name__ == "__main__":
    main = Main()

    main()

example of usage:

$ python test.py foo
'foo'
$ python test.py bar
'bar'
羞稚 2024-10-18 01:15:51

如果“方法”指的是“实例方法”:您无法使用实例调用实例方法,因此您首先需要一个实例。然后,您只需引用 obj.method 并获取一个 BoundMethod 来记住实例 (obj) - 例如:

class Cls:
    def __init__(self, x):
        self.x = x
    def foo(self, new_x):
        self.x = new_x

obj = Cls(1)
foo = obj.foo
foo(2) # same as obj.foo(), but we don't need to keep obj for this!
print(obj.x) #=>2

如果它是静态的 (如果是这样,为什么?99%的自由函数使用起来更简单并且工作得同样好)或类方法,只需参考Cls.method

If by "method" you mean "instance method": You can't call an instance method with instance, so you need an instance first. Then, you can just refer to obj.method and get a BoundMethod which remembers the instance (obj) - e.g.:

class Cls:
    def __init__(self, x):
        self.x = x
    def foo(self, new_x):
        self.x = new_x

obj = Cls(1)
foo = obj.foo
foo(2) # same as obj.foo(), but we don't need to keep obj for this!
print(obj.x) #=>2

If it's a static (if so, why? in 99% free functions are simpler to use and work just as well) or class method, just refer to Cls.method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文