I am trying to create a class which has a static method which returns list of its own instances. How can I do this without referring to the class name:
1: class MyCar(object):
2: def __init__(self, number):
3: self._num = number
4:
5: @staticmethod
6: def get_cars_from(start=0, end=10):
7: """ This method get a list of cars from number 1 to 10.
8: """
9: return_list = []
10: for i in range(start, end):
11: instance = MyCar(i)
12: return_list.append(instance)
13: return return_list
This code works perfectly fine. But I have to reuse this code (copy+paste) in various classes, like Bus, Ship, Plane, Truck.
I am looking for a way to reuse above code in all these classes by making a generic way of instantiating instance of current class. Basically replace line #11 from:
11: instance = MyCar(i)
to a more generic state which can be reused in any class. How can I do this ?