import random
from urllib.request import urlopen
import sys
# 得到外部网址文件地址,并赋值给WORD_URL
WORD_URL = "http://learncodethehardway.org/words.txt"
# 定义空列表WORD
WORDS = []
'''定义字典PHRASES,其内容是解释类的格式:
class %%%(%%%): # 创建一个叫%%%的类,它是%%%的一种
class %%%(object): # 定义类
class %%%(object): def __init__(self,***) # 对类进行初始化,初始化作用是调用这个函数,对新创建的对象进行初始化
class %%%(object): def ***(self,@@@) # 定义***的函数,形参self,实参@@@
***.%%%() # 将***设置为类%%%的一个实例
***.***(@@@) # 从实例***中找到***函数,使用self和实参@@@调用它
***.*** = '***' 从实例***中获取***属性,并将其设为'***'
个人对这个类的定义不认同,容易造成混淆
'''
PHRASES = {
"class %%%(%%%):":
"Make a class named %%% that is-a %%%,",
"class %%%(object):\n\tdef __init__(self, ***)":
"class %%% has-a __init__ that takes self and *** parameters.",
"class %%%(object):\n\tdef ***(self, @@@)":
"class %%% has-a function named *** that takes self and @@@ parameters.",
"*** = %%%()":
"Set *** to an instance of class %%%.",
"***.***(@@@)":
"From *** get the *** function, and call it with parameters self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'"
}
# do they want to drill phrases first
PHRASES_FIRST = False
# 需要输入两个参数,代码名称和english
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASES_FIRST = True
# 从指定网址读取所有行,并将行依次存入word中
for word in urlopen(WORD_URL).readlines():
# Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
# bytes.decode(b'str') 用于byte转str
# 将网址各行转化为WORDS列表,每行为一个元素
WORDS.append(bytes.decode(word.strip()))
# 定义convert函数,参数snippet和phrase
def convert
ex41——学习面向对象术语
最新推荐文章于 2020-11-02 23:43:57 发布