将过程转换为面向对象:Python3.4
为了更加“pythonic”(并且更加模块化),我想将我的过程代码转换为将过程转换为面向对象:Python3.4,python,class,oop,Python,Class,Oop,为了更加“pythonic”(并且更加模块化),我想将我的过程代码转换为类。这里感兴趣的对象是丈夫和妻子的婚姻,这里称为姓氏,每个配偶都有一些共同和独特的属性。这些属性通过yield从文件填充到生成器对象中 我的一部分人希望将这两个解析函数get_human和get_human(可能还有最后的zip函数)作为一个类函数来执行,该类函数仍然使用迭代器对象来填充marriage.firstname,marriage.husbandscore,marriage.wifescore,的属性,然后,我将创
类
。这里感兴趣的对象是丈夫和妻子的婚姻,这里称为姓氏,每个配偶都有一些共同和独特的属性。这些属性通过yield
从文件填充到生成器对象中
我的一部分人希望将这两个解析函数get_human
和get_human
(可能还有最后的zip函数)作为一个类函数来执行,该类函数仍然使用迭代器对象来填充marriage.firstname
,marriage.husbandscore
,marriage.wifescore
,的属性,然后,我将创建一些方法,在从生成器中吐出每个对象时,对这些对象的信息执行进一步的分析
但我的一部分不确定解析定义是否最好作为方法保存,因为我将返回self
,这不是我下游工作所需的生成器
from itertools import izip
def get_husband(husbandfile):
lastname, firstname, husbandscore = '', '', ''
for line in husbandfile.readlines():
# Parse files to fill out the names, wifescores in wifefile
yield name, husbandscore
def get_wife(wifefile):
lastname, firstname, wifescore = '', '',''
for line in wifefile.readlines():
# Parse files to fill out the names, wifescores in wifefile
yield name, wifescore
def make_family(husbandfile, wifefile):
for husband, wife in izip(get_husband(husbandfile), get_wife(wifefile)):
if husband[0] == wife[0]:
lastname = husband[0]
parentscore = husband[1] + wife[0]
yield lastname, parentscore
else:
raise Exception("These two aren't married
你认为把它作为一个类来考虑的最好方法是什么,我可以这样说:min\u score=marriage.parentscore.min()
这段代码非常“Pythonic”。然而,没有明确的原因而转换为OOP是不可能的。这段代码相当“Pythonic”。然而,没有明确的原因而转换为OOP是不可能的。