Python的object()函数不接受参数错误

62

我简直不敢相信这其实是一个问题,但我一直试图调试这个错误,却毫无进展。我肯定是错过了什么非常简单的东西,因为这看起来太愚蠢了。

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']

什么是 Experiences,以及什么是 Experiences.ttypes - BrenBarn
经验已导入 - eatonphil
抱歉,addExperience是一个类方法。这两个包装器(除其他事项外)使其可以访问self - 该变量被重命名为“thrift”。 - eatonphil
2
@phileaton:属于哪个类的类方法?我怀疑Experience(exp_dict)并不是你认为的那个类。你能否创建一个自包含的示例来演示问题? - BrenBarn
2
你能展示一下异常的完整回溯吗?除非Experience在包含addExperience的命名空间中被重新定义,否则该异常是没有意义的。 - Blckknght
显示剩余6条评论
4个回答

91

你混合使用了制表符和空格。实际上,__init__ 方法是嵌套在另一个方法中定义的,所以你的类没有自己的 __init__ 方法,而是继承了 object.__init__ 方法。请在记事本中打开你的代码,而不是其他编辑器,这样你就会看到 Python 的制表符处理规则看到的代码。

这就是为什么你永远不应该混用制表符和空格。坚持使用其中的一种, 推荐使用空格。


49

我对此感到困惑了一段时间。对于__init__来说,这是一个愚蠢的规则。它是两个下划线紧挨在一起组成"__"。


6
因为我太草率了,只写了__init(),所以出现了这个错误。 - citynorman
在我的情况下,init() - 真是个糟糕的错误 :D 自己花了1小时找到解决方法,而在 Stack Overflow 上只用了3分钟 :P - pbaranski

34

我也出现了这个错误。恰巧,我打错了__init__中的字母,写成了__int__

我认为,在许多打错情况下,我使用的IDE(IntelliJ)会将其颜色更改为函数定义的默认设置。但是,在我的情况下,__int__是另一种dunder/magic方法,颜色仍然与IDE显示的__init__相同(默认的预定义项定义颜色),这使我花费了一些时间来发现缺少了i


2
哈哈,我也犯了同样的错误,找不到问题所在。你的评论帮助我交叉检查了我使用的默认方法。谢谢 :) - Nandakishore
简直不敢相信 Stack Overflow 甚至可以帮你解决 错别字 的问题。干杯! - laur

12
你必须在每次敲击和(_)键时按两次,它看起来必须像这样:
__init__

4
题目中的__init__方法正确。 - igauravsehrawat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接