HFBrowning - 1 year ago 69
Python Question

# Do method outputs need to be bound to "self" in order to be used by other methods?

I am very new to object-oriented programming in Python and I am working to implement the accepted answer to this question in Python (it's originally in R).

I have a simple question - is it possible to access the output of one method for use in another method without first binding the output to

`self`
? I presume the answer is "no" - but I also imagine there is some technique that accomplishes the same task that I am not thinking of.

My start to the code is below. It works fine until you get to the
`kappa`
method. I would really like to be able to define
`kappa`
as a simple extension to
`curvature`
(since it's just the absolute value of the same) but I'm not particularly interested in adding it the list of attributes. I may just been overthinking this too, and either something like a closure is possible in Python or adding to the attribute list is the Pythonic thing to do?

``````import numpy as np
from scipy.interpolate import InterpolatedUnivariateSpline

def __init__(self, x, y): #x, y are lists
# Raw data
self.x = x
self.y = y

# Calculate and set cubic spline functions
n = range(1, len(x)+1)
fx = InterpolatedUnivariateSpline(n, x, k=3)
fy = InterpolatedUnivariateSpline(n, y, k=3)
self.fx = fx
self.fy = fy

def curvature(self, t):
# Calculate and return the curvature
xp = self.fx.derivative(); yp = self.fy.derivative()
xpp = xp.derivative(); ypp = yp.derivative()
vel = np.sqrt(xp(t)**2 + yp(t)**2)                  #Velocity
curv = (xp(t)*ypp(t) - yp(t)*xpp(t)) / (vel**3)     #Signed curvature

return curv

def kappa(self, t):
return abs(curv)
``````

``````class Road(object):