Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

object not callable python when parsing a json response

Tags:

python

I have a response from a URL which is of this format.

'history': {'all': [[u'09 Aug', 1,5'],[u'16 Aug', 2, 6]]}

And code is :

response = urllib.urlopen(url)
data = json.loads(response.read())
print data["fixture_history"]['all']
customObject = MyObject (
                           history = data["history"]['all']
                        )

Printing works but in my custom class I am seeing this error :

    history = data["history"]['all']
TypeError: 'module' object is not callable

My class is :

class MyObject:
    #init
    def _init_(self,history):
        self.hstory = history
like image 843
ghostrider Avatar asked Feb 15 '26 13:02

ghostrider


2 Answers

Printing works but in my custom class I am seeing this error : TypeError: 'module' object is not callable

I bet your your class is defined in a module named MyObject.py and that you imported it as import MyObject instead of from MyObject import MyObject, so in your calling code, name MyObject is bound to the module, not the class.

like image 82
bruno desthuilliers Avatar answered Feb 17 '26 01:02

bruno desthuilliers


If you Class is defined in a different Module please make sure that that you have imported it the right way ie. you need to use from X import Y format but not Import X and expect it to work as if we do it that way we need to let python know the module we are calling it from.

And i am not very sure but i think the typo in the constructor might case the issue as stated bigOTHER

like image 34
Yashwin Gokavarapu Avatar answered Feb 17 '26 01:02

Yashwin Gokavarapu