In the code below, all pairs of functions are very similar:set function    rankListId is differentrankListName is differentclass of Mongoengine is differentget functionthe rankListId is differentHow can I make these function into just one function?def set_male_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_male_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengMaleCharts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_male_rank_list(self):    return self.rankListClient.getRankList(, zhineng_male_rank_list)def set_female_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_female_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengFemaleCharts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_female_rank_list(self):    return self.rankListClient.getRankList(, zhineng_female_rank_list)def set_salary0_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_salary0_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengSalary0Charts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_salary0_rank_list(self):    return self.rankListClient.getRankList(, zhineng_salary0_rank_list)def set_salary5_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_salary5_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengSalary5Charts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_salary5_rank_list(self):    return self.rankListClient.getRankList(, zhineng_salary5_rank_list)def set_salary5_inc_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_salary5_inc_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengSalary5IncCharts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_salary5_inc_rank_list(self):    return self.rankListClient.getRankList(, zhineng_salary5_inc_rank_list)def set_salary5_inc_ratio_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_salary5_inc_ratio_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengSalary5IncRatioCharts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_salary5_inc_ratio_rank_list(self):    return self.rankListClient.getRankList(, zhineng_salary5_inc_ratio_rank_list)def set_stability_asc_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_stability_asc_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengYearsDesc.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_stability_asc_rank_list(self):    return self.rankListClient.getRankList(, zhineng_stability_asc_rank_list)def set_stability_desc_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_stability_desc_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengYearsAsc.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_stability_desc_rank_list(self):    return self.rankListClient.getRankList(, zhineng_stability_desc_rank_list)def set_major_ratio_asc_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_major_ratio_asc_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengMajorsAsc.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_major_ratio_asc_rank_list(self):    return self.rankListClient.getRankList(, zhineng_major_ratio_asc_rank_list)def set_major_ratio_desc_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_major_ratio_desc_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengMajorsDesc.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_major_ratio_desc_rank_list(self):    return self.rankListClient.getRankList(, zhineng_major_ratio_desc_rank_list)def set_male_priority_rank_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_male_priority_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengMaleMinusFemaleCharts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_male_priority_rank_list(self):    return self.rankListClient.getRankList(, zhineng_male_priority_rank_list)def set_female_priority_list(self):    rankList              = RankList()    rankList.rankListId   = zhineng_male_priority_rank_list    rankList.rankListName = u    rankList.itemList     = []    charts = ZhinengFemaleMinusMaleCharts.objects.all()[:10]    for c in charts:        rankList.itemList.append(RankListItem(c.zhineng_id, c.zhineng_id))    self.rankListClient.setRankList(, rankList)    return rankListdef get_female_priority_rank_list(self):    return self.rankListClient.getRankList(, zhineng_female_priority_rank_list)
Top-10 lists of occupations, ranked by various attributes
python;statistics;mongodb
I think there are more issues here than just these methods you'vepasted.  I'll first address the overall idea here and then discusschanges you can make without changing the interface.Data driven designYou have several methods here, each only different in theirparametrisation, not their actual behaviour.  This is a huge red flagand should be addressed immediately.Now how you do that is another thing.  Below I'll refactor it into amore functional approach and under the assumption that the interfacecan't change because of external code not under your control.However I'd also like to suggest another idea here, that might, or mightnot be viable for you.Since the (not visible) container class has so many methods it doesn'tlook very nice, the names are repeated all over the place and the endresult are horrible names.  How about rather than having severalget/set_*_rank_list methods, you'd expose wrapper objects with twomethods instead. So e.g.class Foo(object):    def __init__(self, ...):        ...        self.male_rank_list = RankListWrapper(            zhineng_male_rank_list,            u,            ZhinengMaleCharts,            self.rank_list_client        )        ...Then the access pattern would look like:foo.male_rank_list.fetch() # instead of foo.get_male_rank_list()foo.male_rank_list.update() # instead of foo.set_male_rank_list()Not the best method names, but still better than get/set.What you get here is a bit more flexibility and a lot of typingreduction.  This could of course also be achieved with moremeta-programming, but IMO this is good enough for what I can gather fromyour code as what your requirements would be.Stylistic changesFollow PEP8.  Meaning the instance variables should have names inlower case with underscores as separators.Why are the RankLists not initialised with their constructor?  Ifyou can't change that, then you could create a wrapper functionlike create_rank_list to address that.The for c in charts loop is verbose, the same thing can be moreeasily achieved with a list comprehension:rankList.itemList = [RankListItem(c.zhineng_id, c.zhineng_id)                     for c in charts]The strange thing here is that RankListItem takes the ID parametertwice.  If possible change that to default to the first parameter.Otherwise you could create a helper function likecreate_rank_list_item(id).At this point you could either go the functional, or the more datadriven route - I opted for the latter in order to more closely mimic theidea described above.  By putting all the parameters for the functionsinto separate definitions (be it classes, dictionaries, plain, or namedtuples) the code gets much shorter and reusable.The end result looks something like this:def create_rank_list(id, name, item_list):    result = RankList()    result.rankListId = id    result.rankListName = name    result.itemList = item_list    return resultdef create_rank_list_item(id):    return RankListItem(id, id)class RankListDefinition(object):    def __init__(self, id, name, charts):        self.id = id        self.name = name        self.charts = chartsMALE_RANK_LIST = RankListDefinition(    zhineng_male_rank_list,    u,    ZhinengMaleCharts)...class Foo(object):    ...    def set_rank_list(self, definition):        charts = definition.charts.objects.all()[:10]        rank_list = create_rank_list(            definition.id,            definition.name,            [create_rank_list_item(c.zhineng_id) for c in charts]        )        self.rankListClient.setRankList(, rank_list)        return rank_list    def get_rank_list(self, definition):        return self.rankListClient.getRankList(, definition.id)    def set_male_rank_list(self):        return self.set_rank_list(MALE_RANK_LIST)    def get_male_rank_list(self):        return self.get_rank_list(MALE_RANK_LIST)    ...As you can see everything is factored into code (get/set_rank_list)and data (MALE_RANK_LIST, ...), so by adding one more definition (andtwo methods in this variant) you're done with adding a new list.Since the wrapper methods are pretty useless I'd still suggest moving tothe indirect approach mentioned above.Also, if all of these methods were called one after another, I'd alsoremove all of them in favour of a loop over a list of rank listdefinitions instead (just saying).