笨办法学Python3 习题41 学习面向对象术语
专有词汇:
类(class):告诉Python创建新类型的东西
对象(object):两个意思,即最基本的东西,或者某样东西的实例
实例(instance):这是让Python创建一个类时得到的东西
def:这是在类里面定义函数的方法
self:在类的函数里,self指代被访问的对象或者实例的一个变量
继承(inheritance):指一个类可以继承另一个类的特性,和父子关系类似
组合(composition): 指一个类可以将别的类作为它的部件构建起来,有点像车子和车轮的关系。
属性(attribute): 类的一个属性,它来自于组合,而且通常是一个变量。
是什么(is-a): 用来描述继承关系,如salmon is-a fish(鲑鱼是一种鱼)
有什么(has-a): 用来描述某个东西是由另外一些东西组成的,或者某个东西有某个特征,如salmon has-a mouth(鲑鱼有一张嘴)
措辞练习
1.创建一个叫???的类,它是Y的一种。
如:calss X(Y): 创建一个叫X的类,它是Y的一种。
2.类???有一个_init_,它接受self和???作为参数。
如:class X(object):
def _init_(self,J):类X有一个_init_,它接收self和J作为参数。
3.类???有一个名为???的函数,它接收self和???作为参数。
如:class X(object):
def M(self,J):类X有一个名为M的函数,它接收self和J作为参数。
4.将foo设为类???的一个实例。
如:foo = X():将foo设为类X的一个实例
5.从foo中找到???函数,并使用self和???参数调用它。
如:foo.M(J):从foo中找到M函数,并使用self和J参数调用它。
6.从foo中获取???属性,并将其设为???。
如:foo.K = Q:从foo中获取K属性,并将其设为Q。
[code]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": PHRASE_FIRST = True else: PHRASE_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 name for word in class_names: result = result.replace("%%%", word, 1) #fake other name 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 PHRASE_FIRST: question, answer = answer, question print(question) input("> ") print(f"ANSWER: {answer}\n\n") except EOFError: print("\nBye")
result = sentence[:]这是复制列表的方法。你正在使用“列表切片(list slicing)”语法[:]对列表中的所有元素进行切片操作。
阅读更多- 笨方法学Python—习题41:学习面向对象术语
- 习题41 学习面向对象术语
- DAY6之Python学习笔记:面向对象术语整理
- Python面向对象高级编程——学习笔记
- 笨办法学习python习题1
- 【Python】学习笔记——-8.3、面向对象高级编程:3.多重继承
- python学习(3):python中的面向对象思想,类和对象
- Python面向对象学习 1 (什么是面向对象,面向对象的应用场景,待更新)
- Python学习(18)面向对象
- python(六)面向对象学习--类
- 【Python】学习笔记——-8.4、面向对象高级编程:4.定制类
- “笨办法学python”学习笔记-习题5
- python学习笔记(七) - 面向对象高级编程
- python学习小结9:面向对象
- 【Python】Learn Python the hard way, ex41 面向对象术语练习
- python学习笔记六之初识面向对象上(基础篇)
- 【Python】学习笔记——-8.5、面向对象高级编程:5.使用枚举类
- python学习之面向对象学习
- python学习----------面向对象2
- Python面向对象学习(2)