2014-04-19 71 views
22

我不能相信這實際上是一個問題,但我一直在試圖調試這個錯誤,而且我已經無處可去。我確信我錯過了一些非常簡單的事情,因爲這看起來很愚蠢。python對象()不需要參數錯誤

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() - 這表示該類經驗的正確的模塊明智的位置:

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

這裏是目錄(經驗):

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'make_place'] 
+0

什麼是經驗'什麼是'Experiences.ttypes'? – BrenBarn

+0

經驗導入 – eatonphil

+0

但我認爲這並不相關。例如,當我從繼承鏈中刪除對象時,錯誤就變成了「TypeError:這個構造函數不帶參數」。 – eatonphil

回答

39

你已經混合了製表符和空格。 __init__實際上是嵌套在另一個方法中的,所以你的類沒有__init__方法,而是調用object.__init__。在記事本中打開你的代碼,而不是你使用的任何編輯器,你會看到你的代碼爲Python的標籤處理規則。

這就是爲什麼你不應該混合標籤和空格。堅持一個或另一個。建議使用空格。

+0

我知道它必須是那樣的愚蠢。感謝智慧! – eatonphil

1

必須按自來水兩次(_)鍵每次,它必須看起來像:

__init__ 
+2

問題包含正確的'__init__'。 – igaurav

16

我掙扎了一會兒這個問題。愚蠢的規則爲__init__。這是兩個「_」在一起是「__」

+4

得到這個錯誤,因爲我很sl and,只寫'__init()' – citynorman

+1

得到這個錯誤,因爲我很sl and,只寫** _init _()** –

+1

得到這個錯誤,因爲我很蠢,並寫了'___ init ___()'。 – kennycoc

0

我也得到了這個錯誤。順便說一句,我輸入__int__而不是__init__

我認爲,在許多錯誤類型的情況下,我使用的IDE(IntelliJ)會將顏色更改爲函數定義的默認設置。但是,在我的情況__int__是另一個dunder /魔術方法,顏色保持一樣的其中一個IDE顯示爲__init__(默認預定義項定義色彩),它花了一些時間在發現失蹤