美文网首页
python 自定义类继承list类

python 自定义类继承list类

作者: Naritheon | 来源:发表于2016-05-10 23:26 被阅读1606次
#! usr/bin/env python
# -*- coding:utf-8 -*-
import os
os.chdir('C:\\Python34\\headfirst\\demo')

class AtheletList(list):
    def __init__(self,name = None, dob = None, times = []):
        list.__init__([])
        self.name = name
        self.dob = dob
        self.extend(times)
    def top3(self):
        return str(sorted(set([sanitize(i) for i in self]))[0:3])

class Athelet:
    def __init__(self,name = None, dob = None, times = []):
        self.name = name
        self.dob = dob
        self.times = times
    def top3(self):
        return str(sorted(set(self.times))[0:3])
    def add_time(self,time_str):
        self.times.append(sanitize(time_str))
    def add_times(self,times):
        self.times.extend([sanitize(i) for i in times])

def sanitize(time_str):
    if ':' in time_str:
        splitter = ':'
    elif '-' in time_str:
        splitter = '-'
    else:
        return time_str
    (min,sec) = time_str.split(splitter)
    return (min + '.' + sec)

def get_data(filename):
    try:
        with open(filename) as f:
            data = f.readline()
            temp = data.strip().split(',')
            name = temp.pop(0)
            dob = temp.pop(0)
            times = [sanitize(i) for i in temp]
            a = Athelet(name,dob,times)
    except FileNotFoundError as error:
        print('File Error:' + error)
        return None
    return a

james = get_data('james2.txt')
print(james.name + "'s best times are:" + james.top3())
print('*'*64)
print(james.name + "'s date of birth is:" + james.dob)
print('*'*64)
print(james.name + "'s time list is:" + str(james.times))
print('*'*64)
print("Add a data to james's time list: '2-15'")
james.add_time('2-15')
print('*'*64)
print(james.name + "'s time list now is:" + str(james.times))
print('*'*64)
print("Add some datas to james's time list: '2:24','2:39','2-17'")
james.add_times(['1-59','2:24','1-48','2-17'])
print('*'*64)
print(james.name + "'s time list now is:" + str(james.times))
print('*'*64)
b = AtheletList('Dean','1992-09-24',['dawdw','daddfrg','grdgrd'])
print(b.name)

详细内容参见:
python中的继承

相关文章

  • python 自定义类继承list类

    详细内容参见:python中的继承

  • 我所了解的ArrayList源码

    继承及接口 首先你要知道List类继承了Collection类,而Collection类继承了Iterator类。...

  • 类的继承顺序

    python2新式类继承object的类python2经典类未继承任何类的python2新式类和python3的继...

  • 一阶段day16-01面向对象

    Python中类支持继承,并且支持多继承 一、继承 1、什么是继承 父类(超类):被继承的类子类:继承父类的类继承...

  • 2018-10-19面向对象和pygame

    类的继承 python中的类支持继承,并且支持多继承 1.什么是继承 父类(超类):被继承的类子类:继承的类继承就...

  • Day16总结:面向对象和pygame

    类的继承 python中类支持继承,并且支持多继承 1.什么是继承 父类(超类):被继承的类子类:去继承父类的类继...

  • python 面向对象和pygame

    一、类的继承 python中类支持继承,并且支持多继承 1.什么是继承父类(超类):被继承的类子类:去继承父类的类...

  • 10.19 day16面向对象和pygame

    1.类的继承python中类 支持继承,并且支持多继承()1.什么是继承父类(超类):被继承的类 子类:继承的类,...

  • Day16-面向对象和pygame

    一、类的继承 python中类支持继承,并且支持多继承 1.什么是继承 父类(超类):被继承的类 子类:去继承父类...

  • 2018-10-19继承、重写、内存管理和认识pygame

    一、类的继承 Python中类支持继承,并且支持多继承 1、什么是继承 父类(超类):被继承的类子类:去继承父类的...

网友评论

      本文标题:python 自定义类继承list类

      本文链接:https://www.haomeiwen.com/subject/ljwyrttx.html