15.笨方法学python 习题41

在这个课程中会学习面向对象的术语,然后巩固练习,就这样...

然后看不懂请百度,老外的思维确实有点难以理解...

专有词汇:

  • 类(class):告诉Python创建新类型的东西。
  • 对象(object):两个意思,即最基本的东西,或者某样东西的实例。
  • 实例(instance):这是让Python创建一个类时得到的东西。
  • def:这是在类里面定义函数的方法。
  • self:在类的函数中,self指代被访问的对象或者实例的一个变量。
  • 继承(inheritance):指一个类可以继承另一个类的特性,和父子关系类似。
  • 组合(composition):指一个类可以将别的类作为它的部件构建起来,有点像车子和车轮的关系。
  • 属性(attribute):类的一个属性,它来自于组合,而且通常是一个变量。
  • 是什么(is-a):用来描述继承关系,如 Salmond is-a Fish(鲑鱼是一种鱼,鱼是父类,鲑鱼是子类)
  • 有什么(has-a):用来描述某个东西是由另外一些东西组成的,或者某个东西有某个特征,如说Salmond has-a mouth(鲑鱼有一张嘴)。 

  措辞练习:

  接下来Zed给我们除了一些代码,以及用来描述代码的句子。

  • class  X(Y):  创建一个将X的类,它是Y的一种,继承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。

  还有一段看起来更懵,不写了,是在看不懂就百度吧。

阅读测试:

这里有一小段Python代码,利用这些代码去记忆上述的专属词汇。

将这段代码命名为oop_test.py,你可以直接运行oop_test.py来练习,熟练之后你就可以用oop_test.py english两个参数来运行,这样可以反向练习。

另外这段代码调用了urllib的库,使用了其中的urlopen函数,该函数是用来打开网站获取文件对象的。


 
 
  1. import random # '''random函数,可以生成随机浮点数,整数,字符串,甚至帮你随机选择列表中的一个元素,打乱一组数据等'''
  2. from urllib.request import urlopen #''' urlopen函数,可以帮助我们打开网址'''
  3. import sys
  4. WORD_URL = "http://learncodethehardway.org/words.txt"
  5. WORDS = []
  6. PHRASES = {
  7. "class %%%(%%%):":
  8. "Make a class named %%% that is-a %%%.",
  9. "class %%%(object):\n\tdef __init__(self,***)":
  10. "class %%% has-a __init__ that takes self and *** params.",
  11. "class %%%(object):\n\tdef ***(self,@@@)":
  12. "class %%% has-a function *** that takes self and @@@ params.",
  13. "*** = %%%()":
  14. "Set *** to an instance of class %%%.",
  15. "***.***(@@@)":
  16. "From *** get the *** function, call it with params self, @@@.",
  17. "***.*** = '***'":
  18. "From *** get the *** attribute and set it to '***'."
  19. }
  20. # do they want to drill phrases first. ///他们想先操练短语吗?
  21. if len(sys.argv) == 2 and sys.argv[ 1] == "english": # '''判断argv参数是否是2个且第二个参数是english,如果是则为真,不是则为假'''
  22. PHRASES_FIRST = True
  23. else:
  24. PHRASES_FIRST = False
  25. # load up the words form the website. ///从网站下载单词。
  26. for word in urlopen(WORD_URL).readlines(): # '''打开网址,读取网址的所有行,每一行为一个元素,以列表的形式保存,读取列表中的所有元素(也就是单词)'''
  27. WORDS.append(str(word.strip(), encoding= "utf-8")) # '''去除单词的首尾空格或换行符,以utf-8编码,以字符串的类型添加到列表WORDS中'''
  28. def convert(snippet, phrases):
  29. class_names = [w.capitalize() for w in random.sample(WORDS, snippet.count( "%%%"))] #''' 先统计snippet中‘%%%’出现的次数n,再随机返回WORDS中n个元素,然后再'''
  30. other_names = random.sample(WORDS, snippet.count( "***"))
  31. results = []
  32. param_names = []
  33. for i in range( 0, snippet.count( "@@@")):
  34. param_count = random.randint( 1, 3)
  35. param_names.append( ', '.join(
  36. random.sample(WORDS, param_count)))
  37. for sentence in snippet, phrase:
  38. result = sentence[:]
  39. #fake class names
  40. for word in class_names:
  41. result = result.replace( "%%%", word, 1)
  42. #fake other names
  43. for word in other_names:
  44. result = result.replace( "***", word, 1)
  45. #fake parameter lists
  46. for word in param_names:
  47. result = result.replace( "@@@", word, 1)
  48. results.append(result)
  49. return results
  50. # keep going until they hit CTRL-D
  51. try:
  52. while True:
  53. snippets = list(PHRASES.keys())
  54. random.shuffle(snippets)
  55. for snippet in snippets:
  56. phrase = PHRASES[snippet]
  57. question, answer = convert(snippet, phrase)
  58. if PHRASES_FIRST:
  59. question, answer = answer, question
  60. print(question)
  61. input( "> ")
  62. print( f"ANSWER: {answer}\n\n")
  63. except EOFError:
  64. print( "\nBye")

运行这段代码,然后把面向对象的专属词汇翻译为日常语言就行,也可以反向练习。

 

END!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值