HFBrowning - 3 months ago 20

Python Question

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`

My start to the code is below. It works fine until you get to the

`kappa`

`kappa`

`curvature`

`import numpy as np`

from scipy.interpolate import InterpolatedUnivariateSpline

class Road(object):

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)

Answer

Just call the other method:

```
class Road(object):
...
def kappa(self, t):
return abs(self.curvature(t=t))
```