I want to draw locations for firm type 0 and 1 from the uniform distribution, and to calculate distances between the two types by using the OOP concept in Python.
class Firm_loc:
def __init__(self,type):
self.type = type
self.random_location()
def random_location(self):
self.location = uniform(0,1), uniform(0,1)
def distance(self,other):
xx = (self.location[0] - other.location[0])**2
yy = (self.location[1] - other.location[1])**2
return sqrt(xx+yy)
firms = [Firm_loc('Firm_type_0') for i in range(5)]
firms.extend=[Firm_loc('Firm_type_1') for i in range(5)]
Question 1: From here, I am stuck. I have to call the distance method to calcuate every distance between the two firm types (i.e. self and other) and save them onto a list. Any help would be appreciated.
Question 2: Would there be any other more shorter, efficient codes, rather than the above?
Question 3: I am trying using the concept of "self and other" in OOP because of the pair-wise calculation for two types. How would the code be rewritten in terms of the traditional procedural approach?
EDIT:
Thanks for the answers, but as for the distances, I have to have 5x5=25 distances as follows:
Firm_type_0 Firm_type_1 Distance
f0_0 f0_1 D(f0_0, f0_1)
f0_0 f1_1 D(f0_0, f1_1)
f0_0 f2_1 D(f0_0, f2_1)
f0_0 f3_1 D(f0_0, f3_1)
...
I think the two answers do not produce those distances.
firms.extend([Firm_loc('Firm_type_1') for i in range(5)]). Otherwise, you are overwriting alist'sextendmethod.pdistorcdist. This will require you storing your data in numpy arrays though.