自定义类,保存成Athlete.py文件
class Athlete:
def __init__(self, a_name, a_dob=None, a_times=[]):
self.name = a_name
self.dob = a_dob
self.times = a_times
在IDLE中调用
>>> import Athlete
>>> sarah = Athlete('Sarah Sweeney', '2002-6-17', ['1.58','2.02','2:56'])
报如下错误:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
sarah = Athlete('Sarah Sweeney', '2002-6-17', ['1.58','2.02','2:56'])
TypeError: 'module' object is not callable
原因,没有导入类的路径
import Athlete
sarah = Athlete.Athlete('Sarah Sweeney', '2002-6-17', ['1.58','2.02','2:56'])
或者
from Athlete import Athlete
sarah = Athlete('Sarah Sweeney', '2002-6-17', ['1.58','2.02','2:56'])