Fix TypeError Unbound Method in Python Class

I’ve created a class called errors for some error analysis in Python. I’m trying to use the just_print method within this class to print some results by passing two arrays, x (years) and y (temperatures), directly to the interpreter.

Getting This Error:

codeTypeError: unbound method just_print() must be called with errors instance as first argument (got ndarray instance instead)

What this means is that Python expects just_print to be called on an instance of the errors class, but I seem to be calling it in a way that Python doesn’t recognize as being tied to an instance. This is because just_print doesn’t include self as the first parameter, which it needs to refer to an instance of the class.

Here’s my code:

code#imports
from pylab import *

# defining error class
class errors(object):
# creates class object
def __init__(self):
pass

# error method
def just_print(x, y): # defining error method
# error analysis code...

And here’s how I’m calling it:

codetest = errors
test.just_print(years, temps)

Solution:

To fix this, I need to:

  1. Add self as the first argument in the just_print method.
  2. Call just_print on an instance of errors, not directly on the class itself.

Here’s the corrected Code:

code# imports
from pylab import *

# defining the error class
class errors(object):
def __init__(self):
pass

def just_print(self, x, y): # added self as the first parameter
# error analysis
n = len(x) # length of the x data
D = sum(x**2) - 1./n * sum(x)**2 # d is the sum of the squares minus the sum squared over n
x_bar = mean(x) # average all x values
p_coeff, residuals, _, _, _ = polyfit(x, y, 1, full=True) # using only the first 2 results from the poly fit returned values

dm_squared = 1./(n-2) * residuals / D # error squared using standard formula
dc_squared = 1./(n-2) * (D / n + x_bar**2) * residuals / D # error squared using standard formula

dm = sqrt(dm_squared) # rooted squared error
dc = sqrt(dc_squared) # rooted squared error

# printing results
print("The value for gradient is")
print("%.3g" % p_coeff[0], "error:", "%.3g" % dm)
print("The value for intercept is")
print("%.3g" % p_coeff[1], "error:", "%.3g" % dc)

# reading in data from data file called wales_temp.txt
f = open("wales_temp.txt")
temps = loadtxt(f, skiprows=8) # skips the first 8 rows as they are information about the data
f.close()
years = linspace(1911, 2012, (2012 - 1911 + 1), dtype=int) # set the years array of equal length so each corresponds to the temp

print("The max temperature for the last 100 years was", "%.3g" % max(temps), "Celsius in", years[argmax(temps)])
print("The min temperature for the last 100 years was", "%.3g" % min(temps), "Celsius in", years[argmin(temps)])

print("The standard deviation of the temperatures over the past 100 years is:", "%.3g" % std(temps))

years1990 = linspace(1990, 2012, (2012 - 1990 + 1), dtype=int)
temps1990 = temps[-len(years1990):]
temps1990_9degs = temps1990[9 < temps1990]
percent = float(len(temps1990_9degs)) / float(len(temps[9 < temps]))

print("The percentage of years with temperatures above 9 degrees after 1990 compared to all the years is:", (100 * percent), "%")

hist(temps, bins=len(years))
hist(temps1990, bins=len(years))
figure("avg temps")
plot(years, temps, "bx")
best_fit = poly1d(polyfit(years, temps, 1))
plot(years, best_fit(years))

# create an instance of the errors class and call just_print
test = errors()
test.just_print(years, temps) # now called on an instance

Explanation of the Changes:

  • I added self to just_print(self, x, y) to mark it as an instance method.
  • I created test = errors() to create an object of errors and then called test.just_print(years, temps) on this object.

This way, Python knows which instance to use for just_print, and I avoid the TypeError. Now just_print can access the instance’s data properly and run as expected.

Related blog posts