Justin Justin - 1 month ago 6
Python Question

AttributeError: 'function' object has no attribute 'index'

I've made function which finds the list including specific word by input value.
I'd like to calling 'index' from 'search' function. But I've got Attribute error message like this ;

if distance.haversine([search.index]['geometry']['coordinates'][1],
[search.index]['geometry']['coordinates'][0],t_dicc['tuits']['coordinates']['latitud'],
t_dicc['tuits']['coordinates']['longitud']<=radius):
**AttributeError: 'function' object has no attribute 'index'**


I've tried to modify many times but it didn't work.

import os
import string
import json
from pprint import pprint
import distance

def main():

f = open('monumentos-reducido.json', 'r')
mo_dicc = json.load(f)

g = open('tuits.json', 'r')
t_dicc = json.load(g)

def search():

word = raw_input("monument name : ")

if(word in value for word in ('nombre')):
try:
StopIteration

if word > 1:

index = next(index for (index, d) in enumerate(mo_dicc['features']) if d["properties"]["nombre"] == word)

pprint(mo_dicc['features'][index])

except StopIteration:

exit()


search()


radius = input("radius(meters) : ")


def search2rad(search):

resultlst = []

if distance.haversine([search.index]['geometry']['coordinates'][1],[search.index]['geometry']['coordinates'][0], t_dicc['tuits']['coordinates']['latitud'], t_dicc['tuits']['coordinates']['longitud']<=radius):
index = next(index for (index, d) in enumerate(t_dicc['tuits']) if d['coordenadas'])
resultlst.append(t_dicc['tuits'][index])
print resultlst

search2rad(search)

Answer

You are making call to search2rad function as search2rad(search) where search is the function. Within search2rad(), you are doing:

if distance.haversine([search.index]['geometry']['coordinates'][1],[search.index]['geometry']['coordinates'][0], t_dicc['tuits']['coordinates']['latitud'], t_dicc['tuits']['coordinates']['longitud']<=radius):

Here you have mentioned search.index. It is raising error since it is function (with no attribute as index).

I think what you want to do is to pass the value returned by search() call to search2rad(). For that, you may do:

 search2rad(search())

But the cleaner way will be to do it like:

 index = search()
 search2rad(index)
Comments