Python面向对象编程 - 一个记事本程序范例(一)
notebook.py
import datetime last_id = 0 class Note:
'''Represent a note in the notebook. Match against a
string in searches and store tags for each note.''' def __init__(self, memo, tags=''):
self.memo = memo
self.tags = tags
self.creation_date = datetime.date.today()
global last_id
last_id += 1
self.id = last_id def match(self, filter):
return filter in self.memo or filter in self.tags class Notebook:
'''Represent a collection of notes that can be tagged,
modified, and searched.''' def __init__(self):
'''Initialize a notebook with an empty list.'''
self.notes = [] def new_note(self, memo, tags=''):
'''Create a new note and add it to the list.'''
self.notes.append(Note(memo, tags)) def _find_note(self, note_id):
'''Locate the note with the given id.'''
for note in self.notes:
if note.id == note_id:
return note
return None def modify_memo(self, note_id, memo):
'''Find the note with the given id and change its
memo to the given value.'''
self._find_note(note_id).memo = memo def modify_tags(self, note_id, tags):
'''Find the note with the given id and change its
tags to the given value.'''
self._find_note(note_id).tags = tags def search(self, filter):
'''Find all notes that match the given filter
string.'''
return [note for note in self.notes if
note.match(filter)]
note_book_test.py
from notebook import Note, Notebook def display_notes(notes):
for note in notes:
print("Id: %d" %(note.id))
print("Memo: %s" %(note.memo))
print("------------------------------------------") n = Notebook()
n.new_note("hello world")
n.new_note("hello again")
print(n.notes)
display_notes(n.notes) print("********************************************")
print("search keyword: hello")
search_notes = n.search("hello")
display_notes(search_notes)
print("********************************************") print("********************************************")
print("search keyword: world")
search_notes = n.search("world")
display_notes(search_notes)
print("********************************************") print("********************************************")
print("after modify note 1:")
n.modify_memo(1, "Hi Master HaKu")
display_notes(n.notes)
print("********************************************")
运行结果:
[<notebook.Note object at 0x02C40E70>, <notebook.Note object at 0x02C40830>]
Id: 1
Memo: hello world
------------------------------------------
Id: 2
Memo: hello again
------------------------------------------
********************************************
search keyword: hello
Id: 1
Memo: hello world
------------------------------------------
Id: 2
Memo: hello again
------------------------------------------
********************************************
********************************************
search keyword: world
Id: 1
Memo: hello world
------------------------------------------
********************************************
********************************************
after modify note 1:
Id: 1
Memo: Hi Master HaKu
------------------------------------------
Id: 2
Memo: hello again
------------------------------------------
********************************************
Python面向对象编程 - 一个记事本程序范例(一)的更多相关文章
- Python面向对象编程 - 一个记事本程序范例(二)
给程序加上控制台菜单 menu.py import sys from notebook import Notebook, Note class Menu: '''Display a menu and ...
- Python面向对象编程扑克牌发牌程序,另含大量Python代码!
1. 题目 编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不 ...
- python面向对象编程进阶
python面向对象编程进阶 一.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 1 ...
- Python面向对象编程(下)
本文主要通过几个实例介绍Python面向对象编程中的封装.继承.多态三大特性. 封装性 我们还是继续来看下上文中的例子,使用Student类创建一个对象,并修改对象的属性.代码如下: #-*- cod ...
- Python 面向对象编程基础
Python 面向对象编程基础 虽然Pthon是解释性语言,但是Pthon可以进行面向对象开发,小到 脚本程序,大到3D游戏,Python都可以做到. 一类: 语法: class 类名: 类属性,方法 ...
- python面向对象编程学习
python面向对象编程 基本概念理解 面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作 ...
- Python面向对象编程——继承与派生
Python面向对象编程--继承与派生 一.初始继承 1.什么是继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创 ...
- 图解python | 面向对象编程
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/56 本文地址:http://www.showmeai.tech/article-det ...
- python 面向对象编程学习
1. 问题:将所有代码放入一个py文件:无法维护 方案:如果将代码才分放到多个py文件,好处: 1. 同一个名字的变量互相不影响 2.易于维护 3.引用模块: import module 2.包:解决 ...
随机推荐
- 【对比分析五】CSS阻塞和JS阻塞
js 的阻塞特性: 所有浏览器在下载 JS 的时候,会阻止一切其他活动,比如其他资源的下载,内容的呈现等等.直到 JS 下载.解析.执行完毕后才开始继续并行下载其他资源并呈现内容.为了提高用户体验,新 ...
- UVALive 5058 Counting BST 数学
B - Counting BST Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit S ...
- DeveloperAppleHelp
UIKit: 1.UIKit User Interface Catalog 视图 View控件 2.View Programming Guide for iOS 视图编程,用代码 构建界面. 3. ...
- mysql数据库cup飙升处理思路
1.先top查看是那一个进程,哪个端口占用CPU多. 2.show processeslist查看是否由于大量并发,锁引起的负载问题. 3.否则,查看慢查询,找出执行时间长的sql:explain分析 ...
- Windows Apache Django 配置
所有刚刚学习Django 都会在这里配置的时候遇到问题.这里我简单秒速一个傻瓜步骤,使得大家更容易上手. 一.环境准备 1.windows 32系统 至少是 XPSP3 版本,注意:apache2.X ...
- THE CUSTOMISER
http://www.wanga.com/cu.php The Customiser incorporates all of the features of Magic Mouse. It also ...
- JTAG/SPI/ISP/ICSP 接口电路
- ZOJ3673:1729
1729 is the natural number following 1728 and preceding 1730. It is also known as the Hardy-Ramanuja ...
- SCRUM黑
来自 :coolshell 这篇文章的原文在这里(原文链接)(下文不是全译,也不是部分译,我只是把其总结,有我自己的发挥,但是原意大致不变),这篇文章完全是在调侃Scrum的,作者第一段就是一个免费声 ...
- jsp中简易版本的图片上传程序
1.下载相应的组件的最新版本 Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载 附加的Commons IO ...