[英]Python Dict w/Array Values Passed as Params?
Alright so I have a dict with keys that point to an array of 5 values. 好吧,所以我有一个字典,它的键指向5个值的数组。 These 5 values I pass to a class to sort out and feed me info etc.
我将这5个值传递给一个类,以整理并向我提供信息等。
So, this works, but my querstion is is there a better way of exytracting the array to pass to the 'weapon' class, or do i just need to 'wepstats[0],wepstats[1]'
etc? 所以,这可行,但是我的疑问是是否有一种更好的方法来提取数组以传递给“武器”类,还是我只需要
'wepstats[0],wepstats[1]'
等? Or is there a better way of going about this? 还是有更好的解决方案? I'm all ears as Im only learning to do this stuff.
我很惊讶,因为我只是在学习做这些东西。
class Main():
def weaponDamage(self):
#print 15 * 2.22
wdb = WeaponDB()
wepstats = wdb.getWeaponStats('Sword')
wep = Weapon(wepstats[0],wepstats[1],wepstats[2],wepstats[3],wepstats[4])
wep2 = Weapon("Sword", 5, 55, 1.55, 2.1)
print wep
print wep2
s = sayThings()
greet = s.Say()
self.crit = wep.getDamageCrtRND()
self.scrit = wep.getDamageSCrtRND()
self.avg = wep.getDamageAvg()
self.high = wep.getDamageHigh()
self.low = wep.getDamageLow()
self.mod = wep.getDamageMod()
self.norm = wep.getDamageNrmRND()
self.name = wep.getWeaponName()
print greet
print "-----------"
print "Name: " + self.name
print "-----------"
print "High: %s" % self.high
print "Low : %s" % self.low
print "Avg : %s" % self.avg
print "Crit: %s" % self.crit
print "--------------------"
EDIT: Should I be making a DB of items in this manner in the first place? 编辑:我应该首先以这种方式制作数据库的项目吗? Is there a more logic method of doing this?
还有更多的逻辑方法可以做到这一点吗?
class WeaponDB():
"""Here be thine weapons of thy holy might"""
def __init__(self):
self.script = {
'None': "Error: No Weapon stats to get selected.",
'Sword': ("Sword", 5, 55, 1.55, 2.1),
}
def getWeaponStats(self, key = 'None'):
try:
return self.script[key]
except KeyError:
return self.script['None']
class Weapon():
def __init__(self, name = "Stick", high = 1, low = 0, critMod = 1, scritMod = 2):
self.rng = randNum()
self.name = name
self.high = high
self.low = low
self.critMod = critMod
self.scritMod = scritMod
def getWeaponName(self):
return self.name
def getDamageHigh(self):
return self.high
def getDamageLow(self):
return self.low
def getDamageMod(self):
return self.critMod
def getDamageSMod(self):
return self.scritMod
etc...
If I understand well you can do something like this: 如果我很了解,您可以执行以下操作:
class Weapon:
def __init__( self, name = 'Stick', high = 1, low = 0 ):
self.name = name
self.high = high
self.low = low
wepstats = ( 'Sword', 5, 55 )
sword = Weapon( *wepstats )
Then if you check your attributes you get: 然后,如果检查属性,则会得到:
>>> sword.name
'Sword'
>>> sword.high
5
>>> sword.low
55
Using *wepstats
you pass the entire tuple as arguments for your constructor. 使用
*wepstats
您可以将整个元组作为构造函数的参数传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.