Answer the question
In order to leave comments, you need to log in
Python. How to get an array of object instances of a class?
I create a class:
class myClass:
x = 0
def myMethod(self):
x+=1
for x in range(1,10):
myArray.append(myClass)
for x in range(1,10):
myArray.append(0)
myArray[-1] = myClass
Answer the question
In order to leave comments, you need to log in
And what error does it give?
Probably, before the loop, you need to indicate that myArray is a list.
myArray = []
myArray.append(myClass)
myArray.append(myClass())
def myMethod(self):
x+=1
>>> class test:
... def __repr__(self):
... return 'object!'
...
>>> [test() for x in range(10)]
[object!, object!, object!, object!, object!, object!, object!, object!, object!, object!]
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question