471,090 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,090 software developers and data experts.

Random Instance generation

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
Jul 18 '05 #1
6 1379
Balaji writes:
Suppose there is a class A:
I want to generate say 100 random instance of it...
Define "random instance".

How can I do that....
I want the instance name to be like a1, a2, a3...


If you just want to make 100 instances of a class, try something
like:

class A(object): pass

for count in range(100):
exec "a%s = A()" % count

That is hardly random, though.

--
Paul McNett
Independent Software Consultant
http://www.paulmcnett.com
Jul 18 '05 #2
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

Jul 18 '05 #3
I don't know what you mean by "random" but one
solution is:

a=[]
a=[A() for n in range(0,100)]

They are a[0], a[1], a[2} and don't resemble
anything like "randomness", but there are 100
of them stored in the list a.

Larry Bates

"Balaji" <ba****@email.arizona.edu> wrote in message
news:49**************************@posting.google.c om...
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

Jul 18 '05 #4
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...


Usually when you feel that you need to generate instances with
certain names you are on the wrong track in translating the
problem you need to solve into a program.

You can always store 100 instances in a list then iterate
over them.

If you need to identify individual instances by name, create
a data structure (the simplest is a dictionary)
that maps a name to an instance.

Istvan.
Jul 18 '05 #5
ba****@email.arizona.edu (Balaji) wrote in message news:<49**************************@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
Jul 18 '05 #6
ba****@email.arizona.edu (Balaji) writes:
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...


You almost certainly want to use an array instead of all those a1,a2,... .
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Nicholas Geraldi | last post: by
1 post views Thread by David Sworder | last post: by
3 posts views Thread by Mike Brown | last post: by
5 posts views Thread by user | last post: by
13 posts views Thread by Roy Gourgi | last post: by
10 posts views Thread by Curt_C [MVP] | last post: by
2 posts views Thread by Jim in Arizona | last post: by
4 posts views Thread by Dimos | last post: by
16 posts views Thread by jason.cipriani | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.