流利的python书例子2-17

时间:2016-03-02 20:06:05

标签: python fluent

我试图在Luciano Ramalho的“Fluent Python”一书中做例子2-17

import bisect
import sys

HAYSTACK = [1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, 30]
NEEDLES = [0, 1, 2, 5, 8, 10, 22, 23, 29, 30, 31]

ROW_FMT = '{0:2d} @ {1:2d}  {2}{0:<2d}'

def demo(bisect_fn):
    for needle in reversed(NEEDLES):
        position = bisect_fn(HAYSTACK, needle)
        offset = position * ' |'
        print(ROW_FMT.format(needle, position, offset))

if __name__ == '__main__':
    if sys.argv[-1] == 'left':
        bisect_fn = bisect.bisect_left
    else:
        bisect_fn = bisect.bisect

    print('DEMO:', bisect_fn.__name__)
    print('haystack ->', ' '.join('%2d' % n for n in HAYSTACK))
    demo(bisect_fn)

这会产生以下输出和错误:

C:\Python\Python35-32\python.exe D:/projects/python/first/src/bisect.py
DEMO: bisect
haystack ->  1  4  5  6  8 12 15 20 21 23 23 26 29 30
Traceback (most recent call last):
  File "D:/projects/python/first/src/bisect.py", line 23, in <module>
    demo(bisect_fn)
  File "D:/projects/python/first/src/bisect.py", line 11, in demo
    position = bisect_fn(HAYSTACK, needle)
TypeError: 'module' object is not callable

它有什么问题?

0 个答案:

没有答案