Python 有没有办法在一个类中缩短大量重复的代码
我有以下代码:Python 有没有办法在一个类中缩短大量重复的代码,python,python-3.x,function,class,shortcut,Python,Python 3.x,Function,Class,Shortcut,我有以下代码: import matplotlib.pyplot as plt ### imports plotting import numpy as np import pandas as pd class Population: def __init__(self,name,population): self.name = name self.population = population self.conservative = s
import matplotlib.pyplot as plt ### imports plotting
import numpy as np
import pandas as pd
class Population:
def __init__(self,name,population):
self.name = name
self.population = population
self.conservative = self.population * 48/100
self.labour = self.population * 30/100
self.libDem = self.population * 12/100
self.green = self.population * 5/100
self.brexit = self.population * 3/100
self.others = self.population * 2/100
def distribution(self):
print(self.conservative)
print(self.labour)
print(self.libDem)
print(self.brexit)
print(self.others)
def rulingParty(self):
parties = {"Conservative":self.conservative,"Labour":self.labour,"LibDem":self.libDem,"Green":self.green,"Brexit":self.brexit,"Other":self.others}
return max(parties, key=parties.get)
def addCon(self,amount, Lab=1/5,LD=1/5,GRN=1/5,BXT=1/5,Others=1/5):
self.conservative += amount
self.labour += -amount * Lab
self.libDem += -amount * LD
self.green += -amount * GRN
self.brexit += -amount * BXT
self.others += -amount * Others
def addLab(self,amount, Con=1/5,LD=1/5,GRN=1/5,BXT=1/5,Others=1/5):
self.labour += amount
self.conservative += -amount * Con
self.libDem += -amount * LD
self.green += -amount * GRN
self.brexit += -amount * BXT
self.others += -amount * Others
def addLD(self,amount, Con=1/5,Lab=1/5,GRN=1/5,BXT=1/5,Others=1/5):
self.libDem += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.green += -amount * GRN
self.brexit += -amount * BXT
self.others += -amount * Others
def addGRN(self,amount, Con=1/5,Lab=1/5,LD=1/5,BXT=1/5,Others=1/5):
self.green += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.libDem += -amount * LD
self.brexit += -amount * BXT
self.others += -amount * Others
def addBXT(self,amount, Con=1/5,Lab=1/5,LD=1/5,GRN=1/5,Others=1/5):
self.brexit += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.libDem += -amount * LD
self.green += -amount * GRN
self.others += -amount * Others
def addOthers(self,amount, Con=1/5,Lab=1/5,LD=1/5,GRN=1/5,BXT=1/5):
self.others += amount
self.conservative += -amount * Con
self.labour += -amount * Lab
self.libDem += -amount * LD
self.green += -amount * GRN
self.brexit += -amount * BXT
c = Population("UK",100000)
d = [c.conservative,c.labour,c.libDem,c.green,c.brexit,c.others]
while not c.rulingParty()=="Labour":
c.addCon(-1000)
d = np.vstack((d,[c.conservative,c.labour,c.libDem,c.green,c.brexit,c.others]))
print(d)
print(np.shape(d)[0])
应该注意的是,这只是我为了提高python技能而做的一个练习,而不是任何形式的政治模拟。我的问题是,有没有办法减少addCon()、addLab()等函数中重复的行数,因为它们都有很多相同的代码
如有任何反馈,将不胜感激
你的,当然有,但这需要一点点额外的工作 首先,我不会将
libDem
和conservative
作为类中的字段。相反,我会让它们成为字典中的键,并将投票作为值。有点像你在执政党的方法。但是在\uuuu init\uuuu
中设置字典,然后您可以重写您的方法
事实上,您可以将它们合并到一个方法中:
def添加方(自身、参与方、金额、调整因素):
自我投票[政党]+=金额
对于自行投票的其他政党:
如果是另一方聚会:
系数=调整系数(另一方,1/5)
自身投票[其他方]-=金额*系数
您可以创建一个字典,将参与方名称作为键,投票作为值。唯一的add方法将名称作为参数获取。然后你反复阅读dict,为给定的名字加上,为其他名字减去(还需要更多的细节)。你能解释一下这个程序做什么吗?这可能属于代码审查,而不是这里。在addOthers
中,您为什么+=-amount*
?为什么不-=amount*
?如何使用init中的值创建字典?self.vots={'conservators':0,'libDems':0,'Greens':0,等等
或者我想你应该把你想要的任何初始值都放在0上。最后一个问题,对不起,我对Python还是相当陌生,调整因子参数在函数中意味着什么。我应该用它做什么?如何使用?在你最初的方法中,你会通过它例如,在参数GRN
中,然后将格林的票数减少amount*GRN
。现在,我们不再为该因子的每一方设置一个参数,(GRN
,Con
等),我们只有一个字典adjustment\u factors
。然后我们从该字典中获取一方的调整因子(使用.get
)时,我们仍然使用1/5
作为默认值。尽管如果因子始终相同且始终1/5
,那么它根本不需要参数。