As another example of a user-defined type, we’ll define a class called Time that records the time of day. The class definition looks like this:

Write a function print_time that takes a Time object and prints it in the form hour:minute:second.

Write a Boolean function after that takes two Time objects, t1 and t2, and returns true if t1 follows t2 chronologically and False otherwise.

class Time:
""" represents the time of day
attributes: hour, minute, second"""
def print_time(self):
print('%d:%d:%d' % (self.hour,self.minute,self.second))
def after(self,t):
if(self.hour < t.hour):
return False
elif(self.hour == t.hour):
if(self.minute < t.minute):
return False
elif(self.minute == t.minute):
if(self.second <= t.second):
return False
else: return True
return True time = Time()
time.hour = 11
time.minute = 59
time.second = 30
time1 = Time()
time1.hour = 11
time1.minute = 59
time1.second = 36
time2 = Time()
time2.hour = 11
time2.minute = 58
time2.second = 55

from Thinking in Python

Classes and functions的更多相关文章

  1. Think Python - Chapter 16 - Classes and functions

    16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...

  2. C#软件设计——小话设计模式原则之:开闭原则OCP

    前言:这篇继续来看看开闭原则.废话少说,直接入正题. 软件设计原则系列文章索引 C#软件设计——小话设计模式原则之:依赖倒置原则DIP C#软件设计——小话设计模式原则之:单一职责原则SRP C#软件 ...

  3. Python演讲笔记1

    参考: 1. The Clean Architecture in Python (Brandon Rhodes) 2. Python Best Practice Patterns (Vladimir ...

  4. 开放封闭原则(Open Closed Principle)

    在面向对象的设计中有很多流行的思想,比如说 "所有的成员变量都应该设置为私有(Private)","要避免使用全局变量(Global Variables)",& ...

  5. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  6. [python实现设计模式]-3.简单工厂模式-触宝开放平台

    预备知识: 开放封闭原则(Open-Closed Principle OCP) Software entities(classes,modules,functions etc) should open ...

  7. React学习笔记---项目构建

    简介 ReactJs由于有FB的支持,得到了社区的极大关注,同时由于ReactJs只希望专一的做好View层次上的工作,所以本身并没有涉及很多周边工具. 今天要介绍一款工具,同时包含一个构建项目模板的 ...

  8. 我们的动机(Our motivation)

    我们的动机(Our motivation) There are many PHP frameworks nowadays, but none of them is like Phalcon (Real ...

  9. iOS.OpenSource.AllInOne

    Open Source Project for iOS 所有和iOS相关的Open Source Project的汇总. 功能点 开源项目   iOS Gallery RMGallery https: ...

随机推荐

  1. Android Touch事件传递机制全面解析(从WMS到View树)

    转眼间近一年没更新博客了,工作一忙起来.非常难有时间来写博客了,因为如今也在从事Android开发相关的工作,因此以后的博文也会很多其它地专注于这一块. 这篇文章准备从源代码层面为大家带来Touch事 ...

  2. easyui datagrid 动态加入、移除editor

    使用easyui 行编辑的时候完毕编辑的功能比較简单,可是假设要依据一个框的值动态改变别的值或者编辑的时候禁用某个框的时候就比較麻烦了. 比方像以下这样:加入行的时候每一个值都是手动输入,改动的时候第 ...

  3. SQL语句将某字段查询出以逗号隔开

    MySQL的sql语句有好多能够省去server端的复杂处理 1.group_concat 这玩意儿能够实现  将一个字段如id查询出来   成为这种格式:121,122,123,124,125,12 ...

  4. nginx源代码分析--从源代码看nginx框架总结

    nginx源代码总结: 1)代码中没有特别绕特别别扭的编码实现.从变量的定义调用函数的实现封装,都非常恰当.比方从函数命名或者变量命名就能够看出来定义的大体意义,函数的基本功能,再好的架构实如今编码习 ...

  5. React-Native系列Android——Native与Javascript通信原理(一)

    React-Native最核心的是Native与Javascript之间的通信,并且是双向通信.Native层到Javascript层,Javascript层到Native层.虽说是两个方向,但实现上 ...

  6. 关于 折半查找 while 条件 &lt; , &lt;=

    int bin_search(int a[],int len,int key) { int low=0; int high=len-1; while(low<=high) //若为low< ...

  7. Oracle 学习笔记 13 -- 控制用户权限

    数据库控制语言的功能室控制用户对数据库的存取权限. 用户对某类数据具有何种操作权限是有DBA决定的.Oracle 通过GRANT语句完毕权限的授予,通过REVOKE语句完毕对权限的收回. 权限分为系统 ...

  8. yolo源码解析(1):代码逻辑

    一. 整体代码逻辑 yolo中源码分为三个部分,\example,\include,以及\src文件夹下都有源代码存在. 结构如下所示 ├── examples │ ├── darknet.c(主程序 ...

  9. mysql实战45讲读书笔记(二) 一条SQL更新语句是如何执行的 极客时间

    前面我们系统了解了一个查询语句的执行流程,并介绍了执行过程中涉及的处理模块.相信你还记得,一条查询语句的执行过程一般是经过连接器.分析器.优化器.执行器等功能模块,最后到达存储引擎. 那么,一条更新语 ...

  10. zookeeper的节点类型

    Znode有两种类型: 短暂(ephemeral):客户端和服务器端断开连接后,创建的节点自己删除 持久(persistent):客户端和服务器端断开连接后,创建的节点不删除 2)Znode有四种形式 ...