Use setattr to set the attribute's name to a variable:
class pkg():
def __init__(self, name):
self.name = name
lista_ = self.dai_lista(lista)
print lista_
for i in lista_:
i = i.rstrip()
setattr(self, 'ris'+i, riso(i))
def dai_lista(self, lista):
print "sono in dai lista"
return lista
class riso():
def __init__(self, nome):
self.nome = nome
def print_nome(self):
print self.nome
lista = ['a', 'b', 'c' ]
pippo = pkg('pippo')
pippo.risa.print_nome()
pippo.risb.print_nome()
pippo.risc.print_nome()
Output:
sono in dai lista
['a', 'b', 'c']
a
b
c
However, although this works, I would consider dynamic creating of instance attributes bad practice. Instead, you should use a dict to store these classes:
class pkg():
def __init__(self, name):
self.name = name
lista_ = self.dai_lista(lista)
print lista_
self.ris = {}
for i in lista_:
i = i.rstrip()
self.ris[i] = riso(i)
def dai_lista(self, lista):
print "sono in dai lista"
return lista
class riso():
def __init__(self, nome):
self.nome = nome
def print_nome(self):
print self.nome
lista = ['a', 'b', 'c' ]
pippo = pkg('pippo')
pippo.ris['a'].print_nome()
pippo.ris['b'].print_nome()
pippo.ris['c'].print_nome()
And the output is the same as if you had used attributes.