Random Instance generation

B

Balaji

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
 
P

Paul McNett

Balaji said:
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.
 
M

Mike C. Fletcher

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']
>>> 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
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
 
L

Larry Bates

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
 
I

Istvan Albert

Balaji said:
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.
 
P

Peter Abel

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
.... pass
....
filter(lambda i:globals().__setitem__('a%d'%i,A()),range(5)) []
a0

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
 
P

Paul Rubin

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,... .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top