简体   繁体   English

Python错误:TypeError:'模块'对象不能为HeadFirst Python代码调用

[英]Python error: TypeError: 'module' object is not callable for HeadFirst Python code

I'm following the tutorial from the HeadFirst Python book. 我正在关注HeadFirst Python书中的教程。 In chapter 7, I get an error message when trying to run the next code: 在第7章中,我在尝试运行下一个代码时收到错误消息:

Athlete class: 运动员班:

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)

and with the next module I do some tests: 在下一个模块中,我做了一些测试:

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

This is the content of the Julie.txt file: 这是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

and it's almost the same with the other files 它与其他文件几乎相同

I should get something like this output: 我应该得到类似这样的输出:

{'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']}

but I get this error message: 但我收到此错误消息:

['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

What am I doing wrong? 我究竟做错了什么?

Your module and your class AthleteList have the same name. 您的模块和您的班级AthleteList具有相同的名称。 The line 这条线

import AthleteList

imports the module and creates a name AthleteList in your current scope that points to the module object. 导入模块并在当前作用域中创建一个名为AthleteList的名称,指向模块对象。 If you want to access the actual class, use 如果要访问实际的类,请使用

AthleteList.AthleteList

In particular, in the line 特别是在线上

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

you are actually accessing the module object and not the class. 您实际上是访问模块对象而不是类。 Try 尝试

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

You module and class AthleteList have the same name. 您的模块和类AthleteList具有相同的名称。 Change: 更改:

import AthleteList

to: 至:

from AthleteList import AthleteList

This now means that you are importing the module object and will not be able to access any module methods you have in AthleteList 现在这意味着您正在导入模块对象,并且无法访问AthleteList任何模块方法

As @Agam said, 正如@Agam所说,

You need this statement in your driver file: from AthleteList import AtheleteList 您需要在驱动程序文件中使用此语句: from AthleteList import AtheleteList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM