目录 | 上一节 (6.4 生成器表达式) | 下一节 (7.2 匿名函数)

7.1 可变参数

本节介绍可变(variadic)参数。有时,可变参数使用 *args**kwargs 进行表示。

可变位置参数(*args

如果一个函数接受任意数量的(位置)参数,那么我们称该函数使用了可变参数(variable arguments)。示例:

  1. def f(x, *args):
  2. ...

函数调用:

  1. f(1,2,3,4,5)

额外的参数作为元组进行传递:

  1. def f(x, *args):
  2. # x -> 1
  3. # args -> (2,3,4,5)

可变关键字参数(**kwargs

一个函数也可以接受任意数量的关键字参数。示例:

  1. def f(x, y, **kwargs):
  2. ...

函数调用:

  1. f(2, 3, flag=True, mode='fast', header='debug')

额外的参数作为字典进行传递:

  1. def f(x, y, **kwargs):
  2. # x -> 2
  3. # y -> 3
  4. # kwargs -> { 'flag': True, 'mode': 'fast', 'header': 'debug' }

可变位置参数与可变关键字参数结合使用

一个函数可以同时接受可变非关键字参数和可变关键字参数。

  1. def f(*args, **kwargs):
  2. ...

函数调用:

  1. f(2, 3, flag=True, mode='fast', header='debug')

这些参数被分为位置参数和关键字参数两部分。

  1. def f(*args, **kwargs):
  2. # args = (2, 3)
  3. # kwargs -> { 'flag': True, 'mode': 'fast', 'header': 'debug' }
  4. ...

上述函数接受任意数量的位置参数和关键字参数。当编写包装器(wrappers)或要将参数传递给另一个函数时使用。

传递元组和字典

元组可扩展为可变参数:

  1. numbers = (2,3,4)
  2. f(1, *numbers) # Same as f(1,2,3,4)

字典也可以扩展为关键字参数:

  1. options = {
  2. 'color' : 'red',
  3. 'delimiter' : ',',
  4. 'width' : 400
  5. }
  6. f(data, **options)
  7. # Same as f(data, color='red', delimiter=',', width=400)

练习

练习 7.1: 可变参数的简单示例

尝试定义下列函数:

  1. >>> def avg(x,*more):
  2. return float(x+sum(more))/(1+len(more))
  3. >>> avg(10,11)
  4. 10.5
  5. >>> avg(3,4,5)
  6. 4.0
  7. >>> avg(1,2,3,4,5,6)
  8. 3.5
  9. >>>

请注意 *more 是如何收集其它所有参数的。 

练习 7.2:将元组和字典作为参数进行传递

假设你从文件中读取数据,并获得一个元组。例如:

  1. >>> data = ('GOOG', 100, 490.1)
  2. >>>

现在,假设你想根据上面的数据创建一个 Stock 对象。如果你直接传 data ,那就行不通了:

  1. >>> from stock import Stock
  2. >>> s = Stock(data)
  3. Traceback (most recent call last):
  4. File "<stdin>", line 1, in <module>
  5. TypeError: __init__() takes exactly 4 arguments (2 given)
  6. >>>

这个问题很容易解决,直接使用 *data 即可。试试看:

  1. >>> s = Stock(*data)
  2. >>> s
  3. Stock('GOOG', 100, 490.1)
  4. >>>

如果你拥有的是一个字典,那么你可以改用 **。示例:

  1. >>> data = { 'name': 'GOOG', 'shares': 100, 'price': 490.1 }
  2. >>> s = Stock(**data)
  3. Stock('GOOG', 100, 490.1)
  4. >>>

练习 7.3:创建实例列表

report.py 程序中,我们使用如下代码创建了一个实例列表:

  1. def read_portfolio(filename):
  2. '''
  3. Read a stock portfolio file into a list of dictionaries with keys
  4. name, shares, and price.
  5. '''
  6. with open(filename) as lines:
  7. portdicts = fileparse.parse_csv(lines,
  8. select=['name','shares','price'],
  9. types=[str,int,float])
  10. portfolio = [ Stock(d['name'], d['shares'], d['price'])
  11. for d in portdicts ]
  12. return Portfolio(portfolio)

我们可以改用 Stock(**d) 来简化代码。请完成修改。

练习 7.4:参数传递

fileparse.parse_csv() 函数具有一些选项,用于更改文件分隔符和错误报告。也许你会想把这些选择暴露给上面的 read_portfolio() 函数。请完成修改:

  1. def read_portfolio(filename, **opts):
  2. '''
  3. Read a stock portfolio file into a list of dictionaries with keys
  4. name, shares, and price.
  5. '''
  6. with open(filename) as lines:
  7. portdicts = fileparse.parse_csv(lines,
  8. select=['name','shares','price'],
  9. types=[str,int,float],
  10. **opts)
  11. portfolio = [ Stock(**d) for d in portdicts ]
  12. return Portfolio(portfolio)

修改完成后,尝试阅读读取一些带有错误的文件:

  1. >>> import report
  2. >>> port = report.read_portfolio('Data/missing.csv')
  3. Row 4: Couldn't convert ['MSFT', '', '51.23']
  4. Row 4: Reason invalid literal for int() with base 10: ''
  5. Row 7: Couldn't convert ['IBM', '', '70.44']
  6. Row 7: Reason invalid literal for int() with base 10: ''
  7. >>>

现在,尝试隐藏错误:

  1. >>> import report
  2. >>> port = report.read_portfolio('Data/missing.csv', silence_errors=True)
  3. >>>

目录 | 上一节 (6.4 生成器表达式) | 下一节 (7.2 匿名函数)