title: Python - use functions from another module in a function
body: <p>How do I use a function or set of functions from another module as a parameter in my own function I'm defining?</p>
  
  <p>I'm trying to write a function that compares one vector <code>q</code> and calculates its distance from each vector in a set of vectors <code>x</code>. The function <code>get_distances</code> should take the one vector <code>q</code>, the set of vectors <code>x</code>, and a parameter <code>dist_method</code>. <code>dist_method</code> is supposed to take any one of the distance calculations from scipy.spatial.distance and use that for the calculation, so I could call the function something like this: <code>distances = get_distances(q, x, 'euclidean')</code> Here's the scipy reference page - <code>get_distances</code> should be able to take any of the distance functions braycurtis, canberra, ... , sqeuclidean, wminkowski:
  <a href="https://docs.scipy.org/doc/scipy/reference/spatial.distance.html" rel="nofollow noreferrer">https://docs.scipy.org/doc/scipy/reference/spatial.distance.html</a></p>
  
  <p>At the top of the <code>file1.py</code> file where this function is located, I've imported scipy.spatial.distance, from which I thought I should have access to functions to use just like distance.euclidean(), but when I call <code>get_distances</code> in the interpreter, I get <code>AttributeError: 'module' object has no attribute 'dist_method'</code>.</p>
  
  <p>I found a lot of answers already like the below that say functions are 'first-class objects' and that I should be able to use them as arguments just like any other argument, and I've tried using the **kwargs concept, but I can't put it all together.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/803616/passing-functions-with-arguments-to-another-function-in-python">Passing functions with arguments to another function in Python?</a></li>
  <li><a href="https://stackoverflow.com/questions/706721/how-do-i-pass-a-method-as-a-parameter-in-python">How do I pass a method as a parameter in Python</a></li>
  <li><a href="https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments" rel="nofollow noreferrer">https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments</a></li>
  </ul>
  
  <p>Can someone help me understand what I'm missing?</p>
  
  <p>KNN.py:</p>
  
  <pre><code>import numpy as np
  import scipy.spatial.distance as dist
  
  def get_distances(q, x, dist_method='euclidean', *args, **kwargs):
      """Query dataset to get distances for KNN
  
      Given a numpy array of vectors x and
      query point q, use dist_method to calculate
      distance from q to each vector in x
  
      Parameters:
          q: tuple
          x: numpy array
          dist_method (optional): distance function from scipy.spatial.distance
  
      Returns: list of distances
      """
  
      return [dist.dist_method(q, x_i) for x_i in x]
  
  def load_samples():
  
      x = np.array([[1, 6],[2, 4],[3, 7],[6, 8],[7, 1],[8, 4]])
  
      y = np.array([[7],[8],[16],[44],[50],[68]])
  
      q = (4, 2)
  
      return x, y, q
  </code></pre>
  
  <p>Here's what I'm doing in the interpreter:</p>
  
  <pre><code>&gt;&gt;&gt; import KNN as knn
  &gt;&gt;&gt; x, y, q = knn.load_samples()
  &gt;&gt;&gt; x
  array([[1, 6],
         [2, 4],
         [3, 7],
         [6, 8],
         [7, 1],
         [8, 4]])
  &gt;&gt;&gt; d = knn.get_distances(q, x, 'cityblock')
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "KNN.py", line 19, in get_distances
      return [dist.dist_method(q, x_i) for x_i in x]
  AttributeError: 'module' object has no attribute 'dist_method'
  </code></pre>