2

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:

  1. Pair of coordinates of a house
  2. 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?

1
  • No, the return statements are not saving the values of cCoord and qCoord to 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 Commented Jul 19, 2017 at 0:26

1 Answer 1

1

You have to declare the variables cCoord and qCoord. Functions do not return variables that you can use. Think of a function as a black box. It can use variables that you give it, but any changes that it makes will not affect anything outside of that function. The return command just means that if you set a variable equal to c_Coord(), than that variable will have the value(s) that the function returns. To fix this, set variables to both of your Coord functions.

cCoord = c_Coord()
qCoord = q_Coord()

The two functions are both run, and now you can use what they returned outside of those functions.

Sign up to request clarification or add additional context in comments.

1 Comment

For clarification, when using 'self', the syntax will be: cCoord = self.c_Coord() qCoord = self.q_Coord()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.