Creating alot of class instances?

Andre Engels andreengels at gmail.com
Sun Jul 5 21:20:38 CEST 2009


On 7/5/09, kk <maymunbeyin at gmail.com> wrote:

>  I am new to Python classes and trying to figure out this particular
>  issue here. I will need to create instances of a class. But at the
>  moment I do not know how many instances I will end up having, in every
>  case it might be different. Most of the documents I read makes this
>  simpl class-student analogy to explain python classes which is fine.
>  But in those examples the number and the names of the instances were
>  known and limited

That's no problem. The only limit to the number of instances of a
class you can create is your memory - and not even that if you don't
need to 'keep' the instances.

>  I will be querying some data and create class instances based on the
>  data I gather. But the problem as I mentioned is that I do not know
>  the names and the number of the end class instances. They will be
>  based on the content of the data. So how can I create class instances
>  within a loop and when the loop is done how can I figure out the list
>  of instances via class membership?  I can track the names by
>  introducing another list but I want to understand the class side of
>  things.
>
>  The solution might be dead simple but I just cannot figure out at the
>  moment.
>
>  For example this is what I need in the simplest form
>
>  class myclass():
>   def __init__(self,name):
>   self.name=name
>
>  for count,data in enumerate(some list):
>   instance_count=myclass()
>   instance_count.name=data
>
>  print instances

Okay, to solve your problem, we add a list containing all the instances:

class myclass():
    def __init__(self,name):
        self.name=name

instances = []

for count,data in enumerate(some list):
    instance_count=myclass()
    instance_count.name=data
    instances.append(instance_count)

print instances

=============================================

However, that won't work because myclass has an __init__ with 2
attributes, so you will have to call it using an attribute:

class myclass():
    def __init__(self,name):
        self.name=name

instances = []

for count,data in enumerate(some list):
    instance_count=myclass(data)
    instances.append(instance_count)

print instances

=============================================

This works, but it can be done better:

First we notice that count is not used at all, so why create it?


class myclass():
    def __init__(self,name):
        self.name=name

instances = []

for data in some list:
    instance_count=myclass(data)
    instances.append(instance_count)

print instances

=============================================

Then, the variable instance_count is created once, then used in the
next line. We can do that at once:


class myclass():
    def __init__(self,name):
        self.name=name

instances = []

for data in some list:
    instances.append(myclass(data))

print instances

====================

Finally, "print instances" does not give very nice looking
information, so I would change this to:

class myclass():
    def __init__(self,name):
        self.name=name

instances = []

for data in some list:
    instances.append(myclass(data))

print (instance.name for instance in instances)

-- 
André Engels, andreengels at gmail.com



More information about the Python-list mailing list