V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Ansen
V2EX  ›  Python

python 3 继承内置 list 对象问题

  •  
  •   Ansen · 2015-10-04 18:18:14 +08:00 · 3341 次点击
    这是一个创建于 3137 天前的主题,其中的信息可能已经有所发展或是发生改变。


    这里怎么没有继承到内置的 list

    ps:

    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)
    
    class AthleteList(list):
        def __int__(self,a_name,a_dob=None,a_times=[]):
            list.__int__([])
            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()
            temp1=data.strip().split(',')
            return(AthleteList(temp1.pop(0),temp1.pop(0),temp1))
        except IOError as ioerr:
            print('File Error:'+str(ioerr))
            return(None)
    
    '''vera=AthleteList('vera vi')
    vera.append('1.31')
    print(vera.top3())
    vera.extend(['2.22','1-21','2:22'])
    print(vera.top3())'''
    
    james=get_coach_data('james2.txt')
    print(james.name+"'s fastest times are:"+str(james.top3()))
    
    第 1 条附言  ·  2015-10-05 09:10:27 +08:00
    抱歉,是拼写错误,手滑了^ _ ^
    9 条回复    2015-10-05 03:56:21 +08:00
    WhiteSaber
        1
    WhiteSaber  
       2015-10-04 18:35:26 +08:00   ❤️ 1
    首先请把你哪个按照 python 送的编辑器里面的行号打开
    其次,他爆的是这句话
    return(AthleteList(temp1.pop(0),temp1.pop(0),temp1))

    根据 list 的文档,要求传入的是一个可迭代对象
    class list([iterable])
    https://docs.python.org/2/library/functions.html?highlight=list#list

    而你代码想要做的是调用他的__init__ ?
    可能是你拼写错误而已
    swustxxl
        2
    swustxxl  
       2015-10-04 20:40:34 +08:00
    @zzbond0517
    这是从 head first python 第 210 页 中原样抄出的代码,要错也是书里面写出了,但个人觉得这本书还是比较靠谱的(看样子每个程序貌似都经过调试的),或许是编书的时候忽略了某些前提,导致程序报错,碍于经验不足,找不出问题。
    python IDLE 好像没有行号
    swustxxl
        3
    swustxxl  
       2015-10-04 20:42:41 +08:00
    代码中, james2.txt 的内容:
    James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16
    swustxxl
        4
    swustxxl  
       2015-10-04 20:45:42 +08:00
    @zzbond0517 关于 list.__int__([])
    书中的解释是:初始化所派生的类
    Kisesy
        5
    Kisesy  
       2015-10-04 20:49:20 +08:00
    把 __int__ 改成 __init__
    swustxxl
        6
    swustxxl  
       2015-10-04 21:07:51 +08:00
    @Kisesy 嗯,确实敲错了,不好意思
    不过继续报错,这回是这样的了
    Traceback (most recent call last):
    File "C:\Python34\AthleteList\AthlelteList.py", line 37, in <module>
    print(james.name + "'s fastest times are: " + str(james.top3()))
    TypeError: can only concatenate list (not "str") to list
    Kisesy
        7
    Kisesy  
       2015-10-04 21:45:12 +08:00
    我测试没错
    输出: James Lee's fastest times are:['2.01', '2.16', '2.22']
    twor2
        8
    twor2  
       2015-10-05 02:48:15 +08:00
    1. 复制你给代码
    2. 去掉注释, 使用代码数据测试
    3. 改__int__成__init__
    4. 2.7 和 3.5 不报错,测试 ok 的。
    twor2
        9
    twor2  
       2015-10-05 03:56:21 +08:00   ❤️ 1
    新手练习

    --
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   6212 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 02:28 · PVG 10:28 · LAX 19:28 · JFK 22:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.