Suppose I have two arrays that may look something like this
import numpy as np
theta_a = np.array([[-4.0, -3.8, -3.9, -4.0, -4.0, -4.0, -5.0, -6.0, -8.0, -10.0],
[-4.1, -3.9, -3.8, -4.1, -4.0, -4.2, -4.8, -6.2, -8.1, -10.1],
[-3.9, -3.6, -3.7, -3.8, -4.1, -4.0, -4.9, -6.0, -8.2, -9.90]])
theta_b = np.array([[-0.5, -0.6, -0.5, -0.5, -0.7, -0.6, -0.9, -1.0, -1.1, -6.0],
[-0.4, -0.9, -0.8, -0.6, -0.7, -0.8, -1.0, -1.0, -1.1, -6.1],
[-0.4, -0.7, -0.7, -0.8, -0.8, -0.7, -0.9, -1.1, -1.2, -5.9]])
I would like to perform an iterator operation on these lists, and I am able to do so by
d_theta_zip = [b - a for a, b in zip(theta_a, theta_b)]
f_theta_zip = np.tan(np.deg2rad(d_theta_zip))
However, for the sake of curiosity (I have yet to dive into the world of classes) and also to clean up my code a bit, I would like to define a class
that does exactly the same
class Tester:
def __init__(self, a, b):
self.a = a
self.b = b
def __call__(self):
d_theta = self.b - self.a
f_theta = np.tan(np.deg2rad(d_theta))
return f_theta
This works perfectly fine, once I set it up like this
test = Tester(theta_a[0][0], theta_b[0][0])
which provides the exact same result as
print(f_theta_zip[0][0])
That being said, I am unable to figure out a way to iterate through the class using something like
test_2 = [Tester(a, b) for a, b in zip(theta_a, theta_b)]
and I end up getting the following error message
TypeError: 'list' object is not callable
- Is there an elegant way of doing this using a
class
?
As I said, I am doing this as an exercise to get to know the class
system better.