create dynamic instance

Andre Alexander Bell post at andre-bell.de
Fri Jul 16 12:37:00 EDT 2010


On 07/16/2010 06:01 PM, Ray wrote:
> 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 ?
>   """


You would have to keep references to your Test objects (untested code):

if __name__ == '__main__':
    test_objects = []
    for x in range(10):
        test_objects[x] = Test()
    print test_objects[0].value
    ...

You may want to rewrite this as a list comprehension

if __name__ == '__main__':
    test_objects = [Test() for i in range(10)]
    print test_objects[0].value


Andre




More information about the Python-list mailing list