【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

  三、练习用代码

 1 import random
 2 from urllib.request import urlopen
 3 import sys
 4 
 5 WORD_URL = "http://learncodethehardway.org/words.txt"
 6 WORDS = []
 7 
 8 PHRASES = {
 9     "class %%%(%%%):":
10       "Make a class named %%% that is-a %%%.",
11     "class %%%(object):\n\tdef __init__(self, ***)":
12       "class %%% has-a __init__ that takes self and *** params.",
13     "class %%%(object):\n\tdef ***(self, @@@)":
14       "class %%% has-a function *** that takes self and @@@ params.",
15     "*** = %%%()":
16       "Set *** to an instance of class %%%.",
17     "***.***(@@@)":
18       "From *** get the *** function, call it with params self, @@@.",
19     "***.*** = '***'":
20       "From *** get the *** attribute and set it to '***'."
21 }
22 
23 # do they want to drill phrases first
24 if len(sys.argv) == 2 and sys.argv[1] =="english":
25     PHRASES_FIRST = True
26 else:
27     PHRASES_FIRST = False
28 
29 # load up the words from the website
30 for word in urlopen(WORD_URL).readlines():
31     WORDS.append(str(word.strip(),encoding="utf-8"))
32 
33 def convert(snippet, phrase):
34     class_names = [w.capitalize() for w in
35                     random.sample(WORDS, snippet.count("%%%"))]
36     other_names = random.sample(WORDS, snippet.count("***"))
37     results = []
38     param_names = []
39 
40     for i in range(0, snippet.count("@@@")):
41         param_count = random.randint(1,3)
42         param_names.append(', '.join(
43             random.sample(WORDS, param_count)))
44 
45     for sentence in snippet, phrase:
46         result = sentence[:]
47 
48         # fake class class_names
49         for word in class_names:
50             result = result.replace("%%%", word, 1)
51 
52         # fake other class_names
53         for word in other_names:
54             result = result.replace("***", word, 1)
55 
56         # fake parameter lists
57         for word in param_names:
58             result = result.replace("@@@", word, 1)
59 
60         results.append(result)
61 
62     return results
63 
64 # keep going until they hit CTRL+D
65 try:
66     while True:
67         snippets = list(PHRASES.keys())
68         random.shuffle(snippets)
69 
70         for snippet in snippets:
71             phrase = PHRASES[snippet]
72             question, answer = convert(snippet, phrase)
73             if PHRASES_FIRST:
74                 question, answer = answer, question
75 
76             print(question)
77 
78             input("> ")
79             print(f"ANSWER: {answer}\n\n")
80 except EOFError:
81     print("\nBye.")
View Code

  四、阅读更多代码

  找到更多的代码,用前述措辞阅读,找到所有带类的文件,然后完成下列步骤:

  1. 针对每一个类,指出它的名称,以及它是继承于哪些类的。

  2. 列出每个类中的所有函数,以及这些函数的参数。

  3. 列出类中用在self上的所有属性。

  4. 针对每一个属性,指出它是来自哪个类。

posted @ 2019-04-08 17:07  RukiRuki  阅读(168)  评论(0编辑  收藏  举报