Random Instance generation

Peter Abel PeterAbel at gmx.net
Wed Sep 15 22:06:07 CEST 2004

balaji at email.arizona.edu (Balaji) wrote in message news:<494182a9.0409141247.608d365f at posting.google.com>...
> Hello eveybody....
> Suppose there is a class A:
> I want to generate say 100 random instance of it...
> How can I do that....
> I want the instance name to be like a1, a2, a3...
> Any ideas.....
> Cheers
> Balaji

>>> class A:
... 	pass
>>> filter(lambda i:globals().__setitem__('a%d'%i,A()),range(5))
>>> a0
<__main__.A instance at 0x00DD8418>
>>> a1
<__main__.A instance at 0x00DDB748>
>>> a2
<__main__.A instance at 0x00C0DD20>
>>> a3
<__main__.A instance at 0x00C0DD50>
>>> a4
<__main__.A instance at 0x00DDB1E0>

Instead of globals() you can take any dictionary you want. 
Instead of filter you can take list comprehension.
Instead of dictionary you can take a list as container.
Instead of what I posted above you can do what ohters posted.
Pyhton lets you do it in any way of your gusto.

Regards Peter

More information about the Python-list mailing list