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.

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" <> wrote in message
news:...
> 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

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.

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.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!