Creating alot of class instances?

Rickard Lindberg ricli85 at gmail.com
Sun Jul 5 16:38:50 EDT 2009


> Thank you soo much for speedy and in detailed help. Your replies
> really cleared out most of the cloud for me. I have one last issue to
> resolve which is something I did not articulate properly, I realize
> now. The last issue is actually automatically naming the instances.
> The reason I was using the "instance_count" is for enumerating the
> actual name of an instance.
>
> For example lets say I have
>
> class MyMaterials:
>
> and my instances might need to look like
>
> material_01
> material_02
> or
> light_01
> light_02
> or
> Mesh_01
> Mesh_02 etc

If you do not know how many instances you are going to create from the
beginning, there is no way for you to know which of the instances you mentioned
above will get created. So having names for all of the instances will not help
you since you will never know what names are "safe" to use.

On the other hand, if you have all instances in a list, you can refer to them
by index and you know exactly how many of them you have.

If you would like to get instances by some name you gave to them, maybe
something like this will work:

def get_instance(name):
    for instance in instance_list:
        if instance.name == name:
            return instance
    return None

Note that this might very well return None if no instance with that particular
name was found.

-- 
Rickard Lindberg



More information about the Python-list mailing list