python object() 不带参数错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23176597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python object() takes no parameters error
提问by eatonphil
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.)
(对应于 addExperience 的两个装饰器是因为这是在声明其类的文件之外定义的。)
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!
所以这对我来说没有任何意义,因为我清楚地向 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
Here is Experience.mro() - which says the correct module-wise location of the class Experience:
这是 Experience.mro() - 它说明了 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']
采纳答案by user2357112 supports Monica
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.
你混合了制表符和空格。__init__
实际上定义为嵌套在另一个方法中,因此您的类没有自己的__init__
方法,而是继承object.__init__
。在记事本中打开你的代码,而不是你使用的任何编辑器,你会看到你的代码,就像 Python 的选项卡处理规则一样。
This is why you should never mix tabs and spaces. Stick to one or the other. Spaces are recommended.
这就是为什么你不应该混合使用制表符和空格的原因。坚持其中之一。建议使用空格。
回答by user5028791
You must press twice on tap and (_) key each time, it must look like:
您必须_每次按两次水龙头和 ( ) 键,它必须如下所示:
__init__
回答by Mike
I struggled for a while about this. Stupid rule for __init__
. It is two "_" together to be "__"
我为此挣扎了一段时间。愚蠢的规则__init__
。两个“_”一起是“__”
回答by Nafeez Quraishi
I too got this error. Incidentally, i typed __int__instead of __init__.
我也有这个错误。顺便说一句,我输入了__int__而不是__init__。
I think, in many mistype cases the IDE i am using (IntelliJ) would have changed the color to the default set for Function definition. But, in my case __int__being another dunder/magic method, color remained same as the one which IDE displays for __init__(default Predefined item definition color), which took me some time in spotting the missing i.
我认为,在许多输入错误的情况下,我使用的 IDE (IntelliJ) 会将颜色更改为函数定义的默认设置。但是,在我的情况下__int__是另一种 dunder/magic 方法,颜色与 IDE 为__init__显示的颜色保持相同(默认预定义项目定义颜色),这花了我一些时间来发现丢失的i。