Python错误:TypeError:'module'对象不可调用 - HeadFirst Python代码

16 投票
4 回答
115959 浏览
提问于 2025-04-16 14:53

我正在跟着《HeadFirst Python》这本书的教程。在第七章,我在运行下面的代码时遇到了错误信息:

Athlete 类:

class AthleteList(list):
    def __init__(self, a_name, a_dob=None, a_times=[]):
        list.__init__([])
        self.name = a_name
        self.dob = a_dob
        self.extend(a_times)

    def top3(self):
        return(sorted(set([sanitize(t) for t in self]))[0:3])

def get_coach_data(filename):
    try:
        with open(filename) as f:
            data = f.readline()
        templ = data.strip().split(',')
        return(AthleteList(templ.pop(0), templ.pop(0), templ))
    except IOError as ioerr:
        print('File error: ' + str(ioerr))
        return(None)

def sanitize(time_string):
    if '-' in time_string:
        splitter = '-'
    elif ':' in time_string:
        splitter = ':'
    else:
        return(time_string)
    (mins, secs) = time_string.split(splitter)
    return(mins + '.' + secs)

接下来我用下一个模块做了一些测试:

import pickle

import AthleteList

def get_coach_data(filename):
    try:
        with open(filename) as f:
            data = f.readline()
        templ = data.strip().split(',')
        return(AthleteList(templ.pop(0), templ.pop(0), templ))
    except IOError as ioerr:
        print('File error (get_coach_data): ' + str(ioerr))
        return(None)

def put_to_store(files_list):
    all_athletes = {}
    for each_file in files_list:
        ath = get_coach_data(each_file)
        all_athletes[ath.name] = ath
    try:
        with open('athletes.pickle', 'wb') as athf:
            pickle.dump(all_athletes, athf)
    except IOError as ioerr:
        print('File error (put_and_store): ' + str(ioerr))
    return(all_athletes)

def get_from_store():
    all_athletes = {}
    try:
        with open('athletes.pickle', 'rb') as athf:
            all_athletes = pickle.load(athf)
    except IOError as ioerr:
        print('File error (get_from_store): ' + str(ioerr))
    return(all_athletes)


print (dir())

the_files = ['sarah.txt','james.txt','mikey.txt','julie.txt']
data = put_to_store(the_files)

data

这是 Julie.txt 文件的内容:

Julie Jones,2002-8-17,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21,3.01,3.02,2:59

其他文件的内容也差不多

我应该得到像这样的输出:

{'James Lee': ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22', '2-
01', '2.01', '2:16'], 'Sarah Sweeney': ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18',
'2:55', '2:55', '2:22', '2-21', '2.22'], 'Julie Jones': ['2.59', '2.11', '2:11', '2:23', '3-
10', '2-23', '3:10', '3.21', '3-21', '3.01', '3.02', '2:59'], 'Mikey McManus': ['2:22', '3.01',
'3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38', '2:40', '2.22', '2-31']}

但是我却收到了这个错误信息:

['AthleteList', '__builtins__', '__cached__', '__doc__', '__file__', '__name__', '__package__', 'get_coach_data', 'get_from_store', 'pickle', 'put_to_store']
Traceback (most recent call last):
  File "C:\Python\workspace\HeadFirst\src\prueba.py", line 41, in <module>
    data = put_to_store(the_files)
  File "C:\Python\workspace\HeadFirst\src\prueba.py", line 19, in put_to_store
    ath = get_coach_data(each_file)
  File "C:\Python\workspace\HeadFirst\src\prueba.py", line 11, in get_coach_data
    return(AthleteList(templ.pop(0), templ.pop(0), templ))
TypeError: 'module' object is not callable

我哪里做错了呢?

4 个回答

2

正如@Agam所说,

在你的驱动文件中,你需要加上这句话:from AthleteList import AtheleteList

14

你的模块和类 AthleteList 叫了一样的名字。你需要把:

import AthleteList

改成:

from AthleteList import AthleteList

这样做之后,你就是在导入这个模块的对象了,这样你就不能使用在 AthleteList 里面定义的任何方法了。

60

你的模块和类 AthleteList 的名字是一样的。那行代码

import AthleteList

导入了这个模块,并在你当前的环境中创建了一个名为 AthleteList 的名字,它指向的是模块对象。如果你想访问真正的类,可以使用

AthleteList.AthleteList

特别是,在这行代码中

return(AthleteList(templ.pop(0), templ.pop(0), templ))

你实际上是在访问模块对象,而不是类。试试

return(AthleteList.AthleteList(templ.pop(0), templ.pop(0), templ))

撰写回答