Random Instance generation

Mike C. Fletcher mcfletch at rogers.com
Tue Sep 14 17:03:33 EDT 2004


from basicproperty import common, propertied

class MyObject( propertied.Propertied ):
    name = common.StringProperty(
       "name", """The "name" of the object by some measure""",
    )

instances = [ MyObject( 'a%d'%(i,)) for i in range(100) ]

Doing it without basicproperty defining a "name" would, of course, 
require you to determine what you mean by having a "name of" a1 (in 
Python, names point to objects from namespaces, they aren't necessarily 
attributes of the objects themselves, so two names can point to one 
object, or an object can be entirely without a name).

For instance:

    instances = dict( [ ('a%d'%(i,),object()) for i in range( 100 ) ] )

would give you a dictionary with 100 named objects in which you could 
then "eval" or "exec" code snippets that rely on those names.

 >>> instances = dict( [ ('a%d'%(i,),object()) for i in range( 100 ) ] )
 >>> instances['a1']
<object object at 0x00C7D3C0>
 >>> eval( '(a2, a8,a4)', instances )
(<object object at 0x00C7D3C8>, <object object at 0x00C7D3F8>, <object 
object at 0x00C7D3D8>)

which is the kind of thing you would do if you were trying to write an 
interpreter.

Good luck,
Mike

Balaji wrote:

>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
>  
>
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com




More information about the Python-list mailing list