Classes and functions
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的更多相关文章
- 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 ...
- C#软件设计——小话设计模式原则之:开闭原则OCP
前言:这篇继续来看看开闭原则.废话少说,直接入正题. 软件设计原则系列文章索引 C#软件设计——小话设计模式原则之:依赖倒置原则DIP C#软件设计——小话设计模式原则之:单一职责原则SRP C#软件 ...
- Python演讲笔记1
参考: 1. The Clean Architecture in Python (Brandon Rhodes) 2. Python Best Practice Patterns (Vladimir ...
- 开放封闭原则(Open Closed Principle)
在面向对象的设计中有很多流行的思想,比如说 "所有的成员变量都应该设置为私有(Private)","要避免使用全局变量(Global Variables)",& ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- [python实现设计模式]-3.简单工厂模式-触宝开放平台
预备知识: 开放封闭原则(Open-Closed Principle OCP) Software entities(classes,modules,functions etc) should open ...
- React学习笔记---项目构建
简介 ReactJs由于有FB的支持,得到了社区的极大关注,同时由于ReactJs只希望专一的做好View层次上的工作,所以本身并没有涉及很多周边工具. 今天要介绍一款工具,同时包含一个构建项目模板的 ...
- 我们的动机(Our motivation)
我们的动机(Our motivation) There are many PHP frameworks nowadays, but none of them is like Phalcon (Real ...
- iOS.OpenSource.AllInOne
Open Source Project for iOS 所有和iOS相关的Open Source Project的汇总. 功能点 开源项目 iOS Gallery RMGallery https: ...
随机推荐
- [React] Understanding setState in componentDidMount to Measure Elements Without Transient UI State
In this lesson we'll explore using setState to synchronously update in componentDidMount. This allow ...
- python爬虫 分页获取图片并下载
--刚接触python2天,想高速上手,就写了个爬虫,写完之后,成就感暴增,用起来顺手多了. 1.源代码 #coding=utf-8 import urllib import re class Pag ...
- MapReduce运行流程具体解释
在hadoop中.每一个mapreduce任务都会被初始化为一个Job. 每一个Job又能够分为两个阶段:map阶段和reduce阶段.这两个阶段分别用两个函数来表示,即map函数和reduce函数. ...
- 继承QWidget的派生类控件不能设置QSS问题解决(使用style()->drawPrimitive(QStyle::PE_Widget,也就是画一个最简单最原始的QWidget,不要牵扯其它这么多东西)
自定义控件时基类用了QWidget,发现qss设置不起作用,需要重载其paintEvent函数即可: 如下代码: void CCustomWidget::paintEvent(QPaintEvent* ...
- POJ 1990 线段树
题意: 思路: 线段树 (一棵就够啦 不像树状数组,还得用两棵) 先对v从小到大排序.每回插入的时候当前的v是最大的,只需要统计它到各个坐标的距离就好了. 里面存两个东西: 这个坐标左边的坐标个数和这 ...
- Django(part2)
admin site:django自带了admin site,我们需要创建能访问site的用户 #以交互的方式创建超级用户 manage.py createsuperuser 如果要把model加到a ...
- 解码URLDecode和编码URLEnCode
在前台往后台传递参数的时候,在前台进行编码,在后台接收参数的时候,用Decode进行解码: 如果url中包含特殊字符如:&.html标签 <tr><td>等导致url无 ...
- array_key_exists()
array_key_exists()方法用于检查键名是否存在数组中. <?php $a=array("name"=>"XC90","tex ...
- OUTLOOK网站直接点击发送邮件
下面的样式是用文字来做链接的:<a href="mailto:邮箱地址" alt="点击此链接给我写信">网页上显示的文字</a> 下面 ...
- TypeError: Cannot use 'in' operator to search for 'length' in....
前台页面读取商品属性是字符串形式,数据库中存储商品属性是集合形式,前台数据存入数据库中数据格式会自动转,后台数据回显到前台数据格式需要手动转换,否则会报异常 错误信息提示: