1.创建新类Athlete,创建两个唯一的对象实例sarah james,他们会继承Athlete类的特性

>>> class Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name=a_name
self.dob=a_dob
self.times=a_times >>> sarah=Athlete('Sarah Sweeney','2002-07-02',['2:58','2.34','1.56'])
>>> james=Athlete('James Jone')
>>> type(sarah)
<class '__main__.Athlete'>
>>> type(james)
<class '__main__.Athlete'>
>>> sarah
<__main__.Athlete object at 0x02DAB970>
>>> james
<__main__.Athlete object at 0x02A29990>
>>> sarah.name
'Sarah Sweeney'
>>> james.name
'James Jone'
>>> sarah.dob
'2002-07-02'
>>> james.dob
>>> sarah.times
['2:58', '2.34', '1.56']
>>> james.times
[]
>>>

2. 类及其对象的应用:senitize不变,定义类Athlete和两个子方法__init__与top3。通过get_coach_data函数进行调用

def senitize(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 Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name=a_name
self.dob=a_dob
self.times=a_times
def top3(self):
return(sorted(set([senitize(t) for t in self.times]))[0:3]) def get_coach_data(filename):
try:
with open(filename) as f:
data=f.readline()
user=data.strip().split(',')
userob=Athlete(user.pop(0),user.pop(0),user)
print(userob.name+"'s fastest times are:" + str(userob.top3()))
except IOError as ioerr:
print('File error' +str (ioerr))
return (None) get_coach_data('sarah2.txt')
get_coach_data('james2.txt')
get_coach_data('mikey2.txt')
get_coach_data('julie2.txt') ========== RESTART: C:/Users/eric/Documents/Python/kelly/kelly2.py ==========
Sarah Sweeney's fastest times are:['2.18', '2.21', '2.22']
James Lee's fastest times are:['2.01', '2.16', '2.22']
Mikey McManus's fastest times are:['2.22', '2.31', '2.38']
Julie Jones's fastest times are:['2.11', '2.23', '2.59']

3.添加2个新的方法给Athlete类,并调用测试

def senitize(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 Athlete:
def __init__(self,a_name,a_dob=None,a_times=[]):
self.name=a_name
self.dob=a_dob
self.times=a_times
def top3(self):
return(sorted(set([senitize(t) for t in self.times]))[0:3])
def add_time(self,time_value):
self.times.append(time_value)
def add_times(self,list_of_times):
self.times.extend(list_of_times) vera=Athlete('VeraName')
vera.add_time('1.31')
print('Object name is:', vera.name)
print(vera.name+"'s top3 is: " + str(vera.top3()))
vera.add_times(['2.12','3.44','3.33'])
print(vera.name+"'s top3 is: " + str(vera.top3()))
========== RESTART: C:/Users/eric/Documents/Python/kelly/kelly3.py ==========
Object name is: VeraName
VeraName's top3 is: ['1.31']
VeraName's top3 is: ['1.31', '2.12', '3.33']
>>>

Python 定制类与其对象的创建和应用的更多相关文章

  1. Python定制类(进阶6)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6411919.html 本文出自:[Edwin博客园] Python定制类(进阶6) 1. python中什么 ...

  2. python定制类(1):__getitem__和slice切片

    python定制类(1):__getitem__和slice切片 1.__getitem__的简单用法: 当一个类中定义了__getitem__方法,那么它的实例对象便拥有了通过下标来索引的能力. c ...

  3. Python初识类与对象

    Python初识类与对象 类与对象 世界观角度分析类与对象 类是一个抽象的概念,而对象是一个实体的存在,对象由类创造而出,每个对象之间互相独立互不影响,一个对象可以同时拥有多个类的方法,实例化就是通过 ...

  4. python的类和对象2(self参数)

    python的类和对象2(self参数) 1.python里面对象的方法都会有self参数,它就相当于C++里面的this指针:绑定方法,据说有了这个参数,Python 再也不会傻傻分不清是哪个对象在 ...

  5. python 定制类

    看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方法我们也知道是为了能让cla ...

  6. 类和对象的创建过程(元类,__new__,__init__,__call__)

    一. type() 1.创建类的两种方式 方式一 class MyClass(object): def func(self,name): print(name) myc = MyClass() pri ...

  7. python 2 类与对象

    1.类与对象的概念 类即类别.种类,是面向对象设计最重要的概念,从一小节我们得知对象是特征与技能的结合体,而类则是一系列对象相似的特征与技能的结合体. 那么问题来了,先有的一个个具体存在的对象(比如一 ...

  8. Python 【类与对象】

    类与对象 把类的个例就叫做实例 (instance),可理解为“实际的例子”类是某个特定的群体,实例是群体中某个具体的个体 Python中的对象等于类和实例的集合:即类可以看作是对象,实例也可以看作是 ...

  9. python的类与对象

    类与对象 1.什么是类 类和函数一样是程序编程的一种方式,在处理某些问题的时候类比函数更加适合让编程变得简化,在python里面函数式编程和类编程都是为了简化代码的一种编程方式,具体应用那个则由具体问 ...

随机推荐

  1. Ubuntu 13.10下Hadoop 2.2 安装、配置、编译(伪分布式)

    1.安装JDK.在此不做解说,上篇博文里已介绍过.http://www.cnblogs.com/lifeinsmile/p/3578677.html 2.配置ssh. ssh服务,用于管理远程Hado ...

  2. haar-like特征(转载)

    浅析人脸检测之Haar分类器方法  [补充] 这是我时隔差不多两年后, 回来编辑这篇文章加的这段补充, 说实话看到这么多评论很是惊讶, 有很多评论不是我不想回复, 真的是时间久了, 很多细节我都忘记了 ...

  3. N Sum

    题目:N Sum 描述: Given an array of integers, return indices of the two numbers such that they add up to ...

  4. 《C标准库》——之<stdarg.h>

    C语言有个很强大的功能,依靠它,实现了printf等这类有着变长参数列表的函数或者宏.它就是在<stdarg.h>里的变长参数. 内容: va_list :它是一个适合保存va_start ...

  5. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. 数据库必会必知 之 SQL四种语言:DDL DML DCL TCL(转)

    今天群里面讨论,DDL 还是 DML,我这种小白还是总结下他们的区别吧. 1. DDL – Data Definition Language 数据库定义语言:定义数据库的结构. 其主要命令有CREAT ...

  7. windows7旗舰版激活密钥永久版免费分享

    windows7之家不仅提供精品Win7教程 给大家,加上这个windows7激活密匙还帮大家解决windows7系统激活问题,包括win7旗舰版 windows7安装版这些. 用的是Windows7 ...

  8. Choosing proper innodb_log_file_size

    If you’re doing significant amount of writes to Innodb tables decent size of innodb_log_file_size is ...

  9. window.parent与window.openner

    window.parent与window.openner区别介绍 作者: 字体:[增加 减小] 类型:转载 今天总结一下js中几个对象的区别和用法,对这几个概念混淆的朋友可以看看 今天总结一下js中几 ...

  10. PowerDesigner的图形工具栏被我关了 怎么才能恢复?就是那个快捷工具栏 图形那个里面有什么放大镜 表 视图什么的

    PowerDesigner的图形工具栏被我关了 怎么才能恢复?就是那个快捷工具栏 图形那个里面有什么放大镜 表 视图什么的   工具-->自定义工具栏-->palette选中