目录 | 上一节 (3.1 脚本) | 下一节 (3.3 错误检查)

3.2 深入函数

尽管函数在早先时候介绍了,但有关函数在更深层次上是如何工作的细节却很少提供。本节旨在填补这些空白,并讨论函数调用约定,作用域规则等问题。

调用函数

考虑以下函数:

  1. def read_prices(filename, debug):
  2. ...

可以使用位置参数调用该函数:

  1. prices = read_prices('prices.csv', True)

或者,可以使用关键字参数调用该函数:

  1. prices = read_prices(filename='prices.csv', debug=True)

默认参数

有时候,你希望参数是可选的,如果是这样,请在函数定义中分配一个默认值。

  1. def read_prices(filename, debug=False):
  2. ...

如果分配了默认值,则参数在函数调用中是可选的。

  1. d = read_prices('prices.csv')
  2. e = read_prices('prices.dat', True)

注意:带有默认值的参数(译注:即关键字参数)必须出现在参数列表的末尾(所有非可选参数都放在最前面)

首选关键字参数作为可选参数

比较以下两种不同的调用风格:

  1. parse_data(data, False, True) # ?????
  2. parse_data(data, ignore_errors=True)
  3. parse_data(data, debug=True)
  4. parse_data(data, debug=True, ignore_errors=True)

在大部分情况下,关键字参数提高了代码的可读性——特别是对于用作标志的参数,或者与可选特性相关的参数。

设计最佳实践

始终为函数参数指定简短但有意义的名称。

使用函数的人可能想要使用关键字调用风格。

  1. d = read_prices('prices.csv', debug=True)

Python 开发工具将会在帮助功能或者帮助文档中显示这些名称。

返回值

return 语句返回一个值:

  1. def square(x):
  2. return x * x

如果没有给出返回值或者 return 语句缺失,那么返回 None

  1. def bar(x):
  2. statements
  3. return
  4. a = bar(4) # a = None
  5. # OR
  6. def foo(x):
  7. statements # No `return`
  8. b = foo(4) # b = None

多个返回值

函数只能返回一个值。但是,通过将返回值放到元组中,函数可以返回多个值:

  1. def divide(a,b):
  2. q = a // b # Quotient
  3. r = a % b # Remainder
  4. return q, r # Return a tuple

用例:

  1. x, y = divide(37,5) # x = 7, y = 2
  2. x = divide(37, 5) # x = (7, 2)

变量作用域

程序给变量赋值:

  1. x = value # Global variable
  2. def foo():
  3. y = value # Local variable

变量赋值发生在函数的内部和外部。定义在函数外部的变量是“全局的”。定义在函数内部的变量是“局部的”。

局部变量

在函数内部赋值的变量是私有的。

  1. def read_portfolio(filename):
  2. portfolio = []
  3. for line in open(filename):
  4. fields = line.split(',')
  5. s = (fields[0], int(fields[1]), float(fields[2]))
  6. portfolio.append(s)
  7. return portfolio

在此示例中,filename, portfolio, line, fieldss 是局部变量。在函数调用之后,这些变量将不会保留或者不可访问。

  1. >>> stocks = read_portfolio('portfolio.csv')
  2. >>> fields
  3. Traceback (most recent call last):
  4. File "<stdin>", line 1, in ?
  5. NameError: name 'fields' is not defined
  6. >>>

局部变量也不能与其它地方的变量冲突。

全局变量

函数可以自由地访问定义在同一文件中的全局变量值。

  1. name = 'Dave'
  2. def greeting():
  3. print('Hello', name) # Using `name` global variable

但是,函数不能修改全局变量:

  1. name = 'Dave'
  2. def spam():
  3. name = 'Guido'
  4. spam()
  5. print(name) # prints 'Dave'

切记:函数中的所有赋值都是局部的

修改全局变量

如果必须修改全局变量,请像下面这样声明它:

  1. name = 'Dave'
  2. def spam():
  3. global name
  4. name = 'Guido' # Changes the global name above

全局声明必须在使用之前出现,并且相应的变量必须与该函数处在同一文件中。看上面这个函数,要知道这是一种糟糕的形式。事实上,如果可以的话,尽量避免使用 global 。如果需要一个函数来修改函数外部的某种状态,最好是使用类来代替(稍后详细介绍)。

参数传递

当调用一个函数的时候,参数变量的传递是引用传递。不拷贝值(参见2.7 节)。如果传递了可变数据类型(如列表,字典),它们可以被原地修改。

  1. def foo(items):
  2. items.append(42) # Modifies the input object
  3. a = [1, 2, 3]
  4. foo(a)
  5. print(a) # [1, 2, 3, 42]

关键点:函数不接受输入参数的拷贝。

重新赋值与修改

确保了解修改值与给变量名重新赋值的细微差别。

  1. def foo(items):
  2. items.append(42) # Modifies the input object
  3. a = [1, 2, 3]
  4. foo(a)
  5. print(a) # [1, 2, 3, 42]
  6. # VS
  7. def bar(items):
  8. items = [4,5,6] # Changes local `items` variable to point to a different object
  9. b = [1, 2, 3]
  10. bar(b)
  11. print(b) # [1, 2, 3]

提醒:变量赋值永远不会重写内存。名称只是被绑定到了新的值上面

练习

本组练习实现的内容可能是本课程最强大的和最难的。有很多步骤,并且过去练习中的许多概念被一次性整合在一起。虽然最后的题解只有大约 25 行的代码,但要花点时间,确保你理解每一个部分。

report.py 的中心部分主要用于读取 CSV 文件。例如,read_portfolio() 函数读取包含投资组合数据的文件,read_prices() 函数读取包含价格数据的文件。在这两个函数中,有很多底层的“精细的”事以及相似的特性。例如,它们都打开一个文件并使用 csv 模块来处理,并且将各种字段转换为新的类型。

如果真的需要对大量的文件进行解析,可能需要清理其中的一些内容使其更通用。这是我们的目标。

通过打开 Work/fileparse.py 文件开始本练习,该文件是我们将要写代码的地方。

练习 3.3:读取 CSV 文件

首先,让我们仅关注将 CSV 文件读入字典列表的问题。在 fileparse.py 中,定义一个如下所示的函数:

  1. # fileparse.py
  2. import csv
  3. def parse_csv(filename):
  4. '''
  5. Parse a CSV file into a list of records
  6. '''
  7. with open(filename) as f:
  8. rows = csv.reader(f)
  9. # Read the file headers
  10. headers = next(rows)
  11. records = []
  12. for row in rows:
  13. if not row: # Skip rows with no data
  14. continue
  15. record = dict(zip(headers, row))
  16. records.append(record)
  17. return records

该函数将 CSV 文件读入字典列表中,但是隐藏了打开文件,使用 csv 模块处理,忽略空行等详细信息。

试试看:

提示: python3 -i fileparse.py.

  1. >>> portfolio = parse_csv('Data/portfolio.csv')
  2. >>> portfolio
  3. [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}]
  4. >>>

这很好,除了不能使用数据做任何有用的计算之外。因为所有的内容都是用字符串表示。我们将马上解决此问题,先让我们继续在此基础上进行构建。

练习 3.4:构建列选择器

在大部分情况下,你只对 CSV 文件中选定的列感兴趣,而不是所有数据。修改 parse_csv() 函数,以便让用户指定任意的列,如下所示:

  1. >>> # Read all of the data
  2. >>> portfolio = parse_csv('Data/portfolio.csv')
  3. >>> portfolio
  4. [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}]
  5. >>> # Read only some of the data
  6. >>> shares_held = parse_csv('Data/portfolio.csv', select=['name','shares'])
  7. >>> shares_held
  8. [{'name': 'AA', 'shares': '100'}, {'name': 'IBM', 'shares': '50'}, {'name': 'CAT', 'shares': '150'}, {'name': 'MSFT', 'shares': '200'}, {'name': 'GE', 'shares': '95'}, {'name': 'MSFT', 'shares': '50'}, {'name': 'IBM', 'shares': '100'}]
  9. >>>

练习 2.23 中给出了列选择器的示例。

然而,这里有一个方法可以做到这一点:

  1. # fileparse.py
  2. import csv
  3. def parse_csv(filename, select=None):
  4. '''
  5. Parse a CSV file into a list of records
  6. '''
  7. with open(filename) as f:
  8. rows = csv.reader(f)
  9. # Read the file headers
  10. headers = next(rows)
  11. # If a column selector was given, find indices of the specified columns.
  12. # Also narrow the set of headers used for resulting dictionaries
  13. if select:
  14. indices = [headers.index(colname) for colname in select]
  15. headers = select
  16. else:
  17. indices = []
  18. records = []
  19. for row in rows:
  20. if not row: # Skip rows with no data
  21. continue
  22. # Filter the row if specific columns were selected
  23. if indices:
  24. row = [ row[index] for index in indices ]
  25. # Make a dictionary
  26. record = dict(zip(headers, row))
  27. records.append(record)
  28. return records

这部分有一些棘手的问题,最重要的一个可能是列选择到行索引的映射。例如,假设输入文件具有以下标题:

  1. >>> headers = ['name', 'date', 'time', 'shares', 'price']
  2. >>>

现在,假设选定的列如下:

  1. >>> select = ['name', 'shares']
  2. >>>

为了执行正确的选择,必须将选择的列名映射到文件中的列索引。这就是该步骤正在执行的操作:

  1. >>> indices = [headers.index(colname) for colname in select ]
  2. >>> indices
  3. [0, 3]
  4. >>>

换句话说,名称(”name” )是第 0 列,股份数目(”shares” )是第 3 列。

当从文件读取数据行的时候,使用索引对其进行过滤:

  1. >>> row = ['AA', '6/11/2007', '9:50am', '100', '32.20' ]
  2. >>> row = [ row[index] for index in indices ]
  3. >>> row
  4. ['AA', '100']
  5. >>>

练习 3.5:执行类型转换

修改 parse_csv() 函数,以便可以选择将类型转换应用到返回数据上。例如:

  1. >>> portfolio = parse_csv('Data/portfolio.csv', types=[str, int, float])
  2. >>> portfolio
  3. [{'price': 32.2, 'name': 'AA', 'shares': 100}, {'price': 91.1, 'name': 'IBM', 'shares': 50}, {'price': 83.44, 'name': 'CAT', 'shares': 150}, {'price': 51.23, 'name': 'MSFT', 'shares': 200}, {'price': 40.37, 'name': 'GE', 'shares': 95}, {'price': 65.1, 'name': 'MSFT', 'shares': 50}, {'price': 70.44, 'name': 'IBM', 'shares': 100}]
  4. >>> shares_held = parse_csv('Data/portfolio.csv', select=['name', 'shares'], types=[str, int])
  5. >>> shares_held
  6. [{'name': 'AA', 'shares': 100}, {'name': 'IBM', 'shares': 50}, {'name': 'CAT', 'shares': 150}, {'name': 'MSFT', 'shares': 200}, {'name': 'GE', 'shares': 95}, {'name': 'MSFT', 'shares': 50}, {'name': 'IBM', 'shares': 100}]
  7. >>>

练习 2.24 中已经对此进行了探索。需要将下列代码片段插入到题解中:

  1. ...
  2. if types:
  3. row = [func(val) for func, val in zip(types, row) ]
  4. ...

练习 3.6:处理无标题的数据

某些 CSV 文件不包含任何的标题信息。例如,prices.csv 文件看起来像下面这样:

  1. "AA",9.22
  2. "AXP",24.85
  3. "BA",44.85
  4. "BAC",11.27
  5. ...

修改 parse_csv() 文件以便通过创建元组列表来处理此类文件。例如:

  1. >>> prices = parse_csv('Data/prices.csv', types=[str,float], has_headers=False)
  2. >>> prices
  3. [('AA', 9.22), ('AXP', 24.85), ('BA', 44.85), ('BAC', 11.27), ('C', 3.72), ('CAT', 35.46), ('CVX', 66.67), ('DD', 28.47), ('DIS', 24.22), ('GE', 13.48), ('GM', 0.75), ('HD', 23.16), ('HPQ', 34.35), ('IBM', 106.28), ('INTC', 15.72), ('JNJ', 55.16), ('JPM', 36.9), ('KFT', 26.11), ('KO', 49.16), ('MCD', 58.99), ('MMM', 57.1), ('MRK', 27.58), ('MSFT', 20.89), ('PFE', 15.19), ('PG', 51.94), ('T', 24.79), ('UTX', 52.61), ('VZ', 29.26), ('WMT', 49.74), ('XOM', 69.35)]
  4. >>>

要执行此更改,需要修改代码以便数据的第一行不被解释为标题行。另外,需要确保不创建字典,因为不再有可用于列名的键。

练习 3.7:选择其它的列分隔符

尽管 CSV 文件非常普遍,但还可能会遇到使用其它列分隔符(如 制表符(tab) 或空格符(space))的文件。例如,如下所示的 Data/portfolio.dat 文件:

  1. name shares price
  2. "AA" 100 32.20
  3. "IBM" 50 91.10
  4. "CAT" 150 83.44
  5. "MSFT" 200 51.23
  6. "GE" 95 40.37
  7. "MSFT" 50 65.10
  8. "IBM" 100 70.44

csv.reader() 函数允许像下面这样指定不同的分隔符:

  1. rows = csv.reader(f, delimiter=' ')

修改 parse_csv() 函数以便也允许修改分隔符。

例如:

  1. >>> portfolio = parse_csv('Data/portfolio.dat', types=[str, int, float], delimiter=' ')
  2. >>> portfolio
  3. [{'price': '32.20', 'name': 'AA', 'shares': '100'}, {'price': '91.10', 'name': 'IBM', 'shares': '50'}, {'price': '83.44', 'name': 'CAT', 'shares': '150'}, {'price': '51.23', 'name': 'MSFT', 'shares': '200'}, {'price': '40.37', 'name': 'GE', 'shares': '95'}, {'price': '65.10', 'name': 'MSFT', 'shares': '50'}, {'price': '70.44', 'name': 'IBM', 'shares': '100'}]
  4. >>>

说明

到目前为止,如果你已经完成,那么你创建了一个非常有用的库函数。你可以使用它去解析任意的 CSV 文件,选择感兴趣的列,执行类型转换,而不用对文件或者 csv 模块的内部工作有太多的担心。

目录 | 上一节 (3.1 脚本) | 下一节 (3.3 错误检查)