开发者

Managing *args variance in calls to functions

Have a method with the following signature:

def foo(self, bar, *uks):
    return other_method(..., uks)

Normally this is called as:

instance.foo(1234, a, b, c, d)

However in some cases I need to do something like this:

p = [a, b, c, d]
instance.foo(1234, p)

At the receiving end this does not work, because other_method sees *args being made up of a single list object instead of simply a [a, b, c, d] list construct. If I t开发者_Go百科ype the method as:

def foo(self, bar, uks = []):
    return other_method(..., uks)

It works, but then I'm forced to do this every time:

instance.foo(1234, [a, b, c, d])

It's not a huge deal I guess, but I just want to know if I'm missing some more pythonic way of doing this?

Thanks!


Python supports unpacking of argument lists to handle exactly this situation. The two following calls are equivalent:

Regular call:

instance.foo(1234, a, b, c, d)

Argument list expansion:

p = [a, b, c, d]
instance.foo(1234, *p)


p = [a, b, c, d]
instance.foo(1234, *p)

The *p form is the crucial part here -- it means "expand sequence p into separate positional arguments".


I think the answers you have here are correct. Here's a fully fleshed out example:

class MyObject(object):
    def foo(self, bar, *uks):
        return self.other_method(1, uks)

    def other_method(self, x, uks):
        print "uks is %r" % (uks,)

# sample data...
a, b, c, d = 'a', 'b', 'c', 'd'

instance = MyObject()

print "Called as separate arguments:"
instance.foo(1234, a, b, c, d)

print "Called as a list:"
p = [a, b, c, d]
instance.foo(1234, *p)

When run, this prints:

Called as separate arguments:
uks is ('a', 'b', 'c', 'd')
Called as a list:
uks is ('a', 'b', 'c', 'd')

You said on Alex's answer that you got ([a, b, c, d],), but I don't see how.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜