[Tutor] generating instance names from a list

Karl Fast karl.fast at pobox.com
Mon Oct 27 23:36:51 EST 2003


I've got a list and want to create an instance for each item in the
list based on the list value. In other words, I want to create a set
of objects based on some list values.


I'm trying to do something like this (which doesn't work):

class SomeClass:
  names = ['alpha', 'beta']

  def somefunc(self):
      for item in self.names:
          self.item = OtherClass()

class OtherClass:
   ....


Obviously that's not going to work. It creates self.item and keeps
reassigning it until the list is exhausted. 

I've had to resort to this, which seems silly.

  def somefunc(self):
      self.alpha = OtherClass()
      self.beta  = OtherClass()


I'm missing something real obvious and it's late and maybe someone
will answer this by the time I wake up tomorrow!


--karl




More information about the Tutor mailing list