本文主要是介绍【Python】“笨办法”学python习题41-面向对象术语的练习,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用以下代码会无限输出类似于:
1.class X(Y):创建一个叫X的类,它是Y的一种
2.class X(object):def init(self,J):类X有一个__init__,它接收self和J作为参数
3.class X(object):def M(self,J):类X有一个名M的函数,它接收self和J作为参数
4.foo = X():将foo设为类X的一个实例
5.foo.M(J):从foo中找到M函数,并使用self和J参数调用它
6.foo.K = Q:从foo中获取K属性,并将其设为Q
来帮助你更好地认识类、对象、实例、属性
测试代码
import random
from urllib.request import urlopen
import sysWORD_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 *** 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
#sys.argv是一个包含命令行参数的列表
if len(sys.argv) == 2 and sys.argv[1] == "english":PHRASES_FIRST = True
else:PHRASES_FIRST = False#从链接中获取单词并填充进WORDS列表
for word in urlopen(WORD_URL).readlines():WORDS.append(str(word.strip(), encoding = "utf-8"))#count()函数用来统计指定元素在目标对象中出现的次数
#random.sample(population, k)
#population表示要从中进行抽样的序列,可以是一个列表、元组或集合等可迭代对象
#k表示要抽取的样本数量,必须是一个非负整数且不大于population的长度
#最终返回一个新的列表,包含了随机抽取的k个元素。
def convert(snippet, phrase):#capitalize()将字符串的第一个字符转换为大写字母,并将所有其他字符(如果有的话)转换为小写#此处对抽到的单词进行这类处理class_names = [w.capitalize() for w inrandom.sample(WORDS, snippet.count("%%%"))]other_names = random.sample(WORDS, snippet.count("***"))results = []param_names = []#random.randint(a, b)#表示闭区间[a,b],生成一个位于闭区间内的随机整数,返回值为生成的随机整数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:#[:]对列表中所有元素进行切片操作#这是python中复制列表的方法result = sentence[:]#str.replace(old, new [, count])中括号中的内容可以没有#用一个新字符或字符串替换字符串中某个字符串中的原有的字符或子串#指定 count后,只有str中前count个旧字符串old被替换for word in class_names:result = result.replace("%%%", word, 1)for word in other_names:result = result.replace("***", word, 1)for word in param_names:result = result.replace("@@@", word, 1)results.append(result)return results# keep going until they answer "yes"
try:while True:snippets = list(PHRASES.keys())#用于将一个列表中的元素打乱顺序,不会生成新的列表random.shuffle(snippets)#遍历列表snippets中的每个元素并赋给snippetfor snippet in snippets:phrase = PHRASES[snippet]question, answer = convert(snippet, phrase)if PHRASES_FIRST:question, answer = answer, questionprint(question)input(">")print(f"ANSWER: {answer}\n\n")
except EOFError:print("\nBye")
运行结果
可以在>后输出answer的结果,通过直接运行的练习了解各个概念。
balance、cart等单词仅作为类名、属性名、参数名等
PS D:\pystudy> & D:/anaconda3/python.exe d:/pystudy/oop_test.py english
class Balance has-a __init__ that takes self and cart parameters.
>
ANSWER: class Balance(object):def __init__(self, cart)From cart get the branch attribute and set it to 'drawer'.
>
ANSWER: cart.branch = 'drawer'
这篇关于【Python】“笨办法”学python习题41-面向对象术语的练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!