Is it possible for a class to create a number of objects n if I assign n a value? Assuming I have 100 cars that are identical and I want to create an object for each car, I could call a class that creates 100 objects (cars). I couldn't find anything about it on the internet and I don't know what exactly to look for. thanks for the replies
            Asked
            
        
        
            Active
            
        
            Viewed 41 times
        
    1 Answers
2
            You could make use of a static factory method:
class Car:
  def __init__(self, ...):
    ...
  
  @classmethod
  def make_car_array(cls, n=100):
    return [Car() for i in range(n)]
and then call
array_of_cars = Car.make_car_array()
 
    
    
        Captain Trojan
        
- 2,800
- 1
- 11
- 28
- 
                    
- 
                    
- 
                    Because the code doesn't work :-) `TypeError: 'type' object cannot be interpreted as an integer`. @classmethod passes the class as 1st argument `def f(cls, *other_args)`. Cf. [this other post](https://stackoverflow.com/questions/136097/difference-between-staticmethod-and-classmethod) – Demi-Lune Aug 29 '22 at 13:55
- 
                    @Demi-Lune You're right, I missed that, my bad. First argument added. – Captain Trojan Aug 29 '22 at 18:01
