1

I'm using vars() to create some object variables 'on the fly'. This doesn't seem to be the cleanest operation. But how else could I generate attribute names of my object without manually defining them (in this case the dict is relatively short but this are just some test data). I'm aware of the problems that come with this practice as described here.

class DerivedProf(Prof):
    def __init__(self,profiel, TAG, code_DB):
       Prof.__init__(self, profiel, TAG)
       self.CountCodes(self.attr, code_DB)

def CountCodes(self, attr, code_DB):
    count  = 0 
    for key, value in code_DB.iteritems():
        if value[0].lower() == 'true':
            for i,p in enumerate(attr):
                if int(attr[i].code2) == value[1]:
                    count += 1
                else:
                    continue
            vars(self)[key] = count 

code_DB = {'code_72': ['true',72],
          'code_74': ['true',74],
          'code_76': ['true',76],
          'code_88': ['true',88]}
Community
  • 1
  • 1
LarsVegas
  • 6,522
  • 10
  • 43
  • 67

1 Answers1

4

Perhaps you're looking for setattr and getattr. You can do setattr(self, key, count) and then later getattr(self, key).

BrenBarn
  • 242,874
  • 37
  • 412
  • 384