Python newbie here. I am writing a class that has a method to calculate the distance between 2 sets of coordinates. This methods takes 2 arguments:
- Pair of coordinates of a house
- Pair of coordinates of a subway station
This is my code:
import pymysql.cursors
class Test(object):
def __init__(self):
self.create_connection()
def create_connection(self):
self.conn = pymysql.connect(host='localhost',
user='root',
password='',
db='testDB',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
self.cursor = self.conn.cursor()
def __del__(self):
self.c_coord()
self.q_coord()
self.calc_dist(cCoord, qCoord)
self.closeDB
def c_coord(self):
sql = "SELECT ID, coordinates from target where coordinates != 'NA'"
self.cursor.execute(sql)
# a dictionary cursor returns every row in the sql statement as a dictionary
cCoord = self.cursor.fetchall()
return cCoord
def q_coord(self):
sql = "SELECT station, coordinates from qSubway"
self.cursor.execute(sql)
qCoord = self.cursor.fetchall()
return qCoord
# return min subway and distance
def calc_dist(self, cCoord, qCoord):
print(cCoord)
print(qCoord)
def closeDB(self):
self.conn.close()
When I run this in python console, this is what I get:
slsu = Test()
slsu.c_coord() [{'ID': 6221530552, 'coordinates': '40.745300,-73.861100'}, ...
slsu.q_coord() [{'station': '21st Street (IND Crosstown Line)', 'coordinates': '40.744591, -73.948674'}, ...
slsu.calc_dist(cCoord, qCoord) Traceback (most recent call last): File "", line 1, in NameError: name 'cCoord' is not defined
I need some help understanding this error and how to fix it? I thought if you pass an argument to the function, it should automatically recognize it?
cCoordandqCoordto those variables, it is simply returning them. You would need to assign those returned values to variables which you then pass to your next function