在Python中调用外部方法时如何解包元组?

在Python中调用外部方法时如何解包元组?,python,arguments,argument-passing,iterable-unpacking,argument-unpacking,Python,Arguments,Argument Passing,Iterable Unpacking,Argument Unpacking,我在类中多次调用外部库的方法,如下所示: class MyClass: const_a = "a" const_b = True const_c = 1 def push(self, pushee): with ExternalLibrary.open(self.const_a, self.const_b, self.const_c) as el: el.push(pushee) def pop(self):

我在类中多次调用外部库的方法,如下所示:

class MyClass:

    const_a = "a"
    const_b = True
    const_c = 1

    def push(self, pushee):
        with ExternalLibrary.open(self.const_a, self.const_b, self.const_c) as el:
            el.push(pushee)

    def pop(self):
        with ExternalLibrary.open(self.const_a, self.const_b, self.const_c) as el:
            return el.pop()

包含
with
语句的行让我感到困扰,因为它们每次都需要将常量作为参数传递。我希望将参数存储在预定义的数据结构(如元组)中,并将其传递到外部库。

您可以执行以下操作:

args = (const_a, const_b, const_c)
ExternalLibrary.open(*args)
*
语法将iterable(元组、列表等)解压为函数调用中的各个参数。还有一个
**
语法用于将字典解包为关键字参数:

kwargs = {'foo': 1, 'bar': 2}
func(**kwargs) # same as func(foo=1, bar=2)
您也可以在同一调用中使用这两种方法,如
func(*args,**kwargs)

您可以执行以下操作:

args = (const_a, const_b, const_c)
ExternalLibrary.open(*args)
*
语法将iterable(元组、列表等)解压为函数调用中的各个参数。还有一个
**
语法用于将字典解包为关键字参数:

kwargs = {'foo': 1, 'bar': 2}
func(**kwargs) # same as func(foo=1, bar=2)

您也可以在同一个调用中使用这两种方法,如
func(*args,**kwargs)

,这是正确的。Python文档将此描述为。这是正确的。Python文档将此描述为。