create dynamic instance

Ray rui.vapps at gmail.com
Fri Jul 16 12:22:16 EDT 2010


On Jul 16, 12:17 pm, MRAB <pyt... at mrabarnett.plus.com> wrote:
> Ray wrote:
> > class Test:
> >   def __init__(self):
> >     self.value=0
> >   def change(self, val):
> >     self.value=val
>
> > if __name__=='__main__':
> >   for x in range(10):
> >     x=Test()
> >   """
> >   the question is how do i call x.value outside of that for loop?
> >   something like
> >   print x.value ?
> >   """
>
> > thanks for any help.
>
> Have you tried it?
>
> Why are you using the same name for the loop variable and the instance
> you're creating?

yes. I need to create instance. the code above is just a example.
on real code, it read a dict, and create the instance from dict.keys()
name.
that dict is build dynamic from database.



More information about the Python-list mailing list