Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python object() takes no parameters error [closed]

Tags:

python

I can't believe this is actually a problem, but I've been trying to debug this error and I've gotten nowhere. I'm sure I'm missing something really simple because this seems so silly.

import Experiences, Places, Countries class Experience(object):      def make_place(self, place):         addr = place["address"]         addr = Places.ttypes.Address(addr["street"], addr["city"], addr["state"], Countries.ttypes._NAMES_TO_VALUES[addr["country"]], addr["zipcode"])         ll = Geocoder.geocode(addr["street"]+", "+addr["city"]+", "+addr["state"]+" "+addr["zipcode"])         place["location"] = Places.ttypes.Location(ll[0].coordinates[0], ll[0].coordinates[1])      def __init__(self, exp_dict):         exp_dict["datetimeInterval"] = Experiences.ttypes.DateTimeInterval(remove(exp_dict, "startTime"), remove(exp_dict, "endTime"))         exp_dict["type"] = Experiences.ttypes.ExperienceType.OPEN         exp_dict["place"] = self.make_place(exp_dict["place"])         self.obj = Experiences.ttypes.Experience(**exp_dict)  @client.request @client.catchClientException def addExperience(thrift, access_token, exp_dict):     experience = Experience(exp_dict)     return thrift.client.addExperience(thrift.CLIENT_KEY, access_token, experience.obj) 

(The two decorators corresponding to addExperience are because this is defined outside of the file where its class is declared.)

The error I'm getting is:

experience = Experience(exp_dict) TypeError: object() takes no parameters 

So this doesn't make any sense to me because I'm clearly declaring a second argument to the init function. Any help would be awesome!

Traceback (most recent call last):   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__     return self.wsgi_app(environ, start_response)   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app     response = self.make_response(self.handle_exception(e))   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception     reraise(exc_type, exc_value, tb)   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app     response = self.full_dispatch_request()   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request     rv = self.handle_user_exception(e)   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception     reraise(exc_type, exc_value, tb)   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request     rv = self.dispatch_request()   File "/Users/phil/Hangify/hy-frontend-server/env/lib/python2.7/site-    packages/flask/app.py", line 1461, in dispatch_request     return self.view_functions[rule.endpoint](**req.view_args)   File "/Users/phil/Hangify/hy-frontend-server/hangify/session.py", line 22, in check_login     return f()   File "/Users/phil/Hangify/hy-frontend-server/hangify/handlers/create.py", line 31, in Handle     res = exp.addExperience(hangify.thrift_interface, access_token, experience)   File "/Users/phil/Hangify/hy-frontend-server/hangify/client/__init__.py", line 22, in decorator     obj = func(client, *args, **kwargs)   File "/Users/phil/Hangify/hy-frontend-server/hangify/client/__init__.py", line 30, in decorator     return func(*args, **kwargs)   File "/Users/phil/Hangify/hy-frontend-server/hangify/client/exp.py", line 39, in addExperience     experience = Experience(exp_dict) TypeError: object() takes no parameters 

Here is Experience.mro() - which says the correct module-wise location of the class Experience:

[<class 'hangify.client.exp.Experience'>, <type 'object'>] 

And here is dir(Experience):

 ['__class__', '__delattr__', '__dict__', '__doc__', '__format__',  '__getattribute__', '__hash__', '__init__', '__module__', '__new__',  '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',  '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'make_place'] 
like image 227
eatonphil Avatar asked Apr 19 '14 23:04

eatonphil


People also ask

What does takes no arguments mean in Python?

The Python "TypeError: Class() takes no arguments" occurs when we forget to define an __init__() method in a class but provide arguments when instantiating it. To solve the error, make sure to define the __init__() (two underscores on each side) method in the class.

Do Python classes take arguments?

Any class method must have self as first argument. (The name can be any valid variable name, but the name self is a widely established convention in Python.) self represents an (arbitrary) instance of the class.

What is meant by init method without parameters?

__init__ Method with No Parameter__init__ method can be used with no parameter. In this case, the values of the instance will be set to the default values or nothing will be done.


1 Answers

You've mixed tabs and spaces. __init__ is actually defined nested inside another method, so your class doesn't have its own __init__ method, and it inherits object.__init__ instead. Open your code in Notepad instead of whatever editor you're using, and you'll see your code as Python's tab-handling rules see it.

This is why you should never mix tabs and spaces. Stick to one or the other. Spaces are recommended.

like image 145
user2357112 supports Monica Avatar answered Sep 19 '22 15:09

user2357112 supports Monica