Reputation: 11
I am importing a class termIndex
. The class works fine on its own, but I am getting a "AttributeError: 'function' object has no attribute 'search' when I call the class's search function from within another function.
from flask import Flask, request
from TermIndex import termIndex
index = termIndex()
index.load(pickleFile) # No problem here
index.search(term) # No problem here
...
@app.route('/data')
def data():
index.search(term) # This is where I get the Attribute Error
I've tried throwing in global index
at the start of the data function but that didn't help.
EDIT: Like so, I get no error:
@app.route('/data')
def data():
index = termIndex()
index.load(pickleFile)
index.search(term)
and like so, I get no error:
index = termIndex()
index.load(pickleFile)
def data():
index.search(term)
data()
Which is why I've tagged flask.
Upvotes: 0
Views: 126
Reputation: 104812
From the error message you describe, it's likely that you are defining an index
function in the ...
part of your code that you've left out of the question. That makes it impossible for data
to see the original index
value, it sees the function instead.
Here's a simpler example of the same issue:
x = "foo bar"
x.split() # this works here, since the name x refers to a string
def x(): pass # this rebinds the name x to a function
x.split() # raises an AttributeError since functions don't have a split method
To fix the problem, you just need to rename either the index
function or rename the index
variable you've shown us.
Upvotes: 1