python object() 不接受参数错误
我真不敢相信居然会出现这样的问题,我一直在尝试调试这个错误,但一点进展都没有。我肯定是漏掉了什么很简单的东西,因为这看起来实在太傻了。
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)
(与addExperience相关的两个装饰器是因为这个函数是在定义它的类的文件之外定义的。)
我遇到的错误是:
experience = Experience(exp_dict)
TypeError: object() takes no parameters
这让我很困惑,因为我明显在init函数里声明了第二个参数。任何帮助都会很棒!
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
这是Experience.mro()的结果 - 它显示了Experience类的正确模块位置:
[<class 'hangify.client.exp.Experience'>, <type 'object'>]
这是dir(Experience)的结果:
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'make_place']
4 个回答
12
你每次都需要按两次触摸屏和 (_) 键,结果应该是这样的:
__init__
34
我也遇到过这个错误。顺便说一下,我把__int__打成了__init__。
我觉得在很多打错的情况下,我用的这个开发工具(IntelliJ)会把颜色改成函数定义的默认颜色。但是在我的情况下,因为__int__也是一个特殊的方法,所以颜色和__init__显示的一样(都是默认的预定义项颜色),这让我花了一些时间才发现少了一个i。
49
我为这个问题纠结了很久。关于__init__
的规则真是让人头疼。它的前面有两个下划线,写成“__”才对。
91
你在代码中混用了制表符和空格。其实,__init__
方法是嵌套在另一个方法里的,所以你的类并没有自己的 __init__
方法,而是继承了 object.__init__
。你可以用记事本打开你的代码,这样就能看到代码在Python处理制表符时的样子。
这就是为什么你绝对不要混用制表符和空格的原因。最好只用其中一种。推荐使用空格。