您的位置:首页 > 编程语言 > Python开发

笨办法学Python3 习题41 学习面向对象术语

2018-10-31 16:46 211 查看
版权声明:True and True True False and True False 1 == 1 and 2 == 1 False "test" == "test" True 1 == 1 or 2 != 1 True True and 1 == 1 True False and 0 != 0 False T https://blog.csdn.net/qq_41718357/article/details/83444405

专有词汇:

类(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)”语法[:]对列表中的所有元素进行切片操作。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: