我也列(zhuang)出(bi)几个高级技巧:
1. contextmanager
写Python代码的时候经常将一系列操作放在一个语句块中,Python 2.5加入了with语法,实现上下文管理功能,这让代码的可读性更强并且错误更少。最常见的例子就是open,如果不使用with,使用open会是这样:
In : f = open('/tmp/a', 'a')
In : f.write('hello world')
In : f.close()
如果使用with,可以简化为两行:
In : with open('/tmp/a', 'a') as f:
....: f.write('hello world')
....:
在执行完缩进的代码块后会自动关闭文件。创建上下文管理器实际就是创建一个类,添加__enter__和__exit__方法。看看如何实现open的上下文管理功能:
class OpenContext(object):
def __init__(self, filename, mode):
self.fp = open(filename, mode)
def __enter__(self):
return self.fp
def __exit__(self, exc_type, exc_val, exc_tb):
self.fp.close()
with OpenContext('/tmp/a', 'a') as f:
f.write('hello world')
自定义上下文管理器确实很方便,但是Python标准库还提供了更易用的上下文管理器工具模块contextlib,它是通过生成器实现的,我们不必再创建类以及__enter__和__exit__这两个特殊的方法:
from contextlib import contextmanager
@contextmanager
def make_open_context(filename, mode):
fp = open(filename, mode)
try:
yield fp
finally:
fp.close()
with make_open_context('/tmp/a', 'a') as f:
f.write('hello world')
yield关键词把上下文分割成两部分:yield之前就是__init__中的代码块;yield之后其实就是__exit__中的代码块;yield生成的值会绑定到with语句as子句中的变量(如果没有生成,也就没有as字句)。
2. total_ordering。对比自定义对象需要添加__lt__、__le__、__gt__、__ge__和__eq__等方法,如果使用total_ordering,只需要定义__eq__以及__lt__、__le__、__gt__、__ge__四种之一就可以了:
In : @functools.total_ordering
...: class Size(object):
...: def __init__(self, value):
...: self.value = value
...: def __lt__(self, other):
...: return self.value < other.value
...: def __eq__(self, other):
...: return self.value == other.value
In : Size(3) > Size(2)
Out: True
In : Size(2) == Size(2)
Out: True
3. 有时候BUG隐藏的太深,需要对上下文都有清晰的展示来帮助判断。用pdb调试不方便,用print不直观。可以使用如下函数获取当前调用栈:
import sys
def get_cur_info():
print sys._getframe().f_code.co_filename # 当前文件名
print sys._getframe(0).f_code.co_name # 当前函数名
print sys._getframe(1).f_code.co_name # 调用该函数的函数的名字,如果没有被调用,则返回module
print sys._getframe().f_lineno # 当前行号
4. inspect。有时候我们想查看一下对象的一些信息或者做类型检查,也就是自省(检查某些事物以确定它是什么、它知道什么以及它能做什么):
In : def add(a, b=1):
...: return a + b
...:
In : inspect.getsourcelines(add)
Out: ([u'def add(a, b=1):\n', u' return a + b\n'], 1)
In : import inspect
In : inspect.getsourcelines(add)
Out: ([u'def add(a, b=1):\n', u' return a + b\n'], 1)
In : inspect.getargspec(add)
Out: ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=(1,))
In : inspect.getcallargs(add, 10, 2)
Out: {'a': 10, 'b': 2}
In : inspect.isclass(add)
Out: False
In : inspect.isfunction(add)
Out: True
5. Mixin模式。它是什么先看 Mixin是什么概念? - Python 。假如我们想通过python内置类型Dict的方式来存放数据,编写的类可以混入DictMixin就好了:
from UserDict import DictMixin
class MyDict(DictMixin):
def __init__(self, dict=None, **kwargs):
self.data = {}
if dict is not None:
self.update(dict)
if len(kwargs):
self.update(kwargs)
def __getitem__(self, id):
return self.data[id]
def __setitem__(self, id, value):
self.data[id] = value
def __delitem__(self, id):
del self.data[id]
def keys(self):
return self.data.keys()
PS: 如果你想兼容Python 2.6以下和Python 3,可以使用collections.MutableMapping:
try:
from UserDict import DictMixin
except ImportError:
from collections import MutableMapping as DictMixin
但是MutableMapping需要额外实现__iter__和__len__。PPS: MutableMapping是学习实现抽象类的范例cpython: 522adc2e082a Lib/_abcoll.py,它继承了Iterable和Sized,而Iterable中通过abstractmethod要求你必须定义__iter__方法(cpython: 522adc2e082a Lib/_abcoll.py),Sized中要求你必须定义__len__ (cpython: 522adc2e082a Lib/_abcoll.py)否则就会提示:
TypeError: Can't instantiate abstract class MyDict with abstract methods __iter__, __len__