Random Instance generation
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.....
>>> class A:
>>> filter(lambda i:globals().__setitem__('a%d'%i,A()),range(5))
<__main__.A instance at 0x00DD8418>
<__main__.A instance at 0x00DDB748>
<__main__.A instance at 0x00C0DD20>
<__main__.A instance at 0x00C0DD50>
<__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.
More information about the Python-list