使用字典作为变量

时间:2013-11-06 18:26:14

标签: python python-2.7

def Stats(self):
    Stats = {}
    for i in self.ExpTable:
      Stats[i] = self.GetLvl(i)
    for i in Stats:
      print i + ":" + str(Stats[i])

我需要能够在定义时使用ExpTable作为变量,因为我需要稍后使用其他dict的所有格式{“String”:Integer}

def Stats(self, {ExpTable}):
    pass

像这样的东西可以用它作为字典,但能够改变dictonaries

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望Stats方法将字典作为参数,但如果没有提供,则使用对象的ExpTable属性。

def Stats(self, d=None):
    if d is None:
        d = self.ExpTable
    new_stats = {}
    for i in d:
        new_stats[i] = self.GetLvl(i)
    for i in new_stats:
        print i + ":" + str(new_stats[i])