【Python基础】lpthw - Exercise 41 学习面向对象术语
一、专有词汇
- 类(class):告诉python创建新类型的东西。
- 对象(object):两个意思,即最基本的东西,或者某样东西的实例。
- 实例(instance):让python创建一个类时得到的东西。
- def:在类中定义函数。
- self:在类的函数中,self指代被访问的对象或实例的一个变量。
- 继承(inheritance):指一个类可以继承另一个类的特性,和父子关系类似。
- 组合(composition):指一个类可以将别的类作为它的部件构建起来。
- 属性(attribute):类的一个属性,它来自于组合,而且通常是一个变量。
- 是什么(is-a):用来描述继承关系。
- 有什么(has-a):用来描述某个东西是由另一些东西组成的,或者某个东西有某个特征。
二、措辞练习
- class X(Y):创建一个叫X的类,它是Y的一种。
- class X(object): def __init__(self, J):类X有一个叫__init__的函数,它以self和J为参数。
- class X(object): def M(self, J):类X有一个叫M的函数,它以self和J为参数。
- foo = X():将foo设为类X的一个实例。
- foo.M(J):从foo中找到M函数,并使用self和J函数调用它。
- foo.K = Q:从foo中获取K属性,并将其设置为Q
三、练习用代码
import random
from urllib.request import urlopen
import sys WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = [] PHRASES = {
"class %%%(%%%):":
"Make a class named %%% that is-a %%%.",
"class %%%(object):\n\tdef __init__(self, ***)":
"class %%% has-a __init__ that takes self and *** params.",
"class %%%(object):\n\tdef ***(self, @@@)":
"class %%% has-a function *** that takes self and @@@ params.",
"*** = %%%()":
"Set *** to an instance of class %%%.",
"***.***(@@@)":
"From *** get the *** function, call it with params self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'."
} # do they want to drill phrases first
if len(sys.argv) == 2 and sys.argv[1] =="english":
PHRASES_FIRST = True
else:
PHRASES_FIRST = False # load up the words from the website
for word in urlopen(WORD_URL).readlines():
WORDS.append(str(word.strip(),encoding="utf-8")) def convert(snippet, phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("%%%"))]
other_names = random.sample(WORDS, snippet.count("***"))
results = []
param_names = [] for i in range(0, snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(
random.sample(WORDS, param_count))) for sentence in snippet, phrase:
result = sentence[:] # fake class class_names
for word in class_names:
result = result.replace("%%%", word, 1) # fake other class_names
for word in other_names:
result = result.replace("***", word, 1) # fake parameter lists
for word in param_names:
result = result.replace("@@@", word, 1) results.append(result) return results # keep going until they hit CTRL+D
try:
while True:
snippets = list(PHRASES.keys())
random.shuffle(snippets) for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASES_FIRST:
question, answer = answer, question print(question) input("> ")
print(f"ANSWER: {answer}\n\n")
except EOFError:
print("\nBye.")
四、阅读更多代码
找到更多的代码,用前述措辞阅读,找到所有带类的文件,然后完成下列步骤:
1. 针对每一个类,指出它的名称,以及它是继承于哪些类的。
2. 列出每个类中的所有函数,以及这些函数的参数。
3. 列出类中用在self上的所有属性。
4. 针对每一个属性,指出它是来自哪个类。
【Python基础】lpthw - Exercise 41 学习面向对象术语的更多相关文章
- python基础-------python2.7教程学习【廖雪峰版】(二)
2017年6月7日14:59:27任务: 看完python基础1.计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文 ...
- Python基础入门(6)- 面向对象编程
1.初识面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本篇随笔将详细介绍Python的面向对象编程. 如果你以前没有接触过面向对象 ...
- Day5 - Python基础5 常用模块学习
Python 之路 Day5 - 常用模块学习 本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shel ...
- <<Python基础课程>>学习笔记 | 文章13章 | 数据库支持
备注:本章介绍了比较简单,只是比较使用样品,主要假设是把握连接,利用数据库.和SQLite做演示样本 ------ Python数据库API 为了解决Python中各种数据库模块间的兼容问题,如今已经 ...
- Python基础5 常用模块学习
本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...
- <<Python基础教程>>学习笔记 | 第10章 | 充电时刻
第10章 | 充电时刻 本章主要介绍模块及其工作机制 ------ 模块 >>> import math >>> math.sin(0) 0.0 模块是程序 一个简 ...
- Py修行路 python基础 (十五)面向对象编程 继承 组合 接口和抽象类
一.前提回忆: 1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均 ...
- Py修行路 python基础 (十六)面向对象编程的 继承 多态与多态性 封装
一.继承顺序: 多继承情况下,有两种方式:深度优先和广度优先 1.py3/py2 新式类的继承:在查找属性时遵循:广度优先 继承顺序是多条分支,按照从左往右的顺序,进行一步一步查找,一个分支走完会走另 ...
- Python基础(18)_面向对象程序设计2(反射、__str__、__del__、__item__系列)
一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo(object) ...
随机推荐
- BootStrap分页教程
https://www.cnblogs.com/laowangc/p/8875526.html https://www.cnblogs.com/yinglunstory/p/6092834.html ...
- pythonのsimple_tag
当我们需要在页面种直接调用py文件中的某些方法时,我们就要用到simple_tag.具体步骤如下: 1.在某个app下创建templatetags文件夹,切记该名称是不可以改变的. 2.在该文件夹下创 ...
- LaTeX技巧892: Ubuntu 安装新版本TeXLive并更新
原文地址:http://www.latexstudio.net/archives/9788.html 摘要: 本文比较系统地介绍了在Ubuntu下的TeXLive的安装与配置测试过程,建议使用Ubun ...
- c++ 智能指针用法详解
本文介绍c++里面的四个智能指针: auto_ptr, shared_ptr, weak_ptr, unique_ptr 其中后三个是c++11支持,并且第一个已经被c++11弃用. 为什么要使用智能 ...
- GIt -- Window下配置 git
全局配置 git config --global user.name "账户名" git config --global use r.email '账户邮箱' 生成ssh,命令 ...
- liunx 修改ssh 端口22
vim /etc/ssh/sshd_config 找到Port 22 添加 Port 5002 重启sshd /bin/systemctl restart sshd.service 防火墙释放5 ...
- Hive快捷查询:不启用Mapreduce job启用Fetch task三种方式介绍
如果查询表的某一列,Hive中默认会启用MapReduce job来完成这个任务,如下: hive>select id,name from m limit 10;--执行时hive会启用MapR ...
- laravel 汇总数据
public function userInfluenceCollect(Request $request) { $types = ['logins', "checkins", & ...
- 4.9cf自训9..
cf401D 状态压缩dp好题,每次把新加入集合的数字放在最后即可 /* 它可以通过重新排列数字n, 它没有任何前导零, x除以m后的余数等于0. 每次把新加的数放在最后 dp[i][j]表示状态i下 ...
- Jmeter性能测试之Monitor监控(SSHMon Samples Collector)
前面写的一篇Monitor监控有缺陷, 这篇文章使用Jmeter4.0+的版本, 使用插件SSHMon Samples Collector来做资源监控 1. 官网下载插件: plugins-manag ...