Hi guys, this is probably a very basic question so excuse me for that.

In java you can say:

Instance instance[] = new Instance[6]
           for (int i =0;i<6;i++){
                instancei[i]=new Instance(...)

Or basically declare lots of instances very quickly.

In python is there an equivalent where I could write:

For i in range (0,6):
               instance[i]=Instance(...)

Or something?

Thanks again.

Yup, having used both languages i generally find python even easier than java to do that.

#Just have a class we can instantiate
class Instance(object):
    x = 1
    def __init__(self, number):
        self.number = number

instances = []
for f in range(10):
    instances.append(Instance(f))

for f in instances:
    print(f.number)

The main difference is that you do not need to use the 'new' operator and also you do not need to say how long the array is going to be so you can just use the append argument which means that the instance will be appended to the list.

Hope that helps

Thanks very much Paul, appreciate it.

You can take Paul's code and simplify it a bit more with list comprehension ...

# a test class we can instantiate
class Instance:
    def __init__(self, number):
        self.number = number

instances = [Instance(n) for n in range(6)]

for f in instances:
    print(f.number)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.