2016-11-30 43 views
-1

我有一個函數返回一個類,並將其另存爲一個對象。當我嘗試在一個類的方法調用我會得到我已經在對象上更名,以確保它是沒有命名衝突錯誤TypeError:調用類函數時不調用'int'對象

location = file_type.route(env['REQUEST_URI']) # Get the location 
TypeError: 'int' object is not callable 

在第一行我調用模塊路由中的方法get_file_type。

file_type = route.get_file_type(env['REQUEST_URI']) # Get File_type object 
location = file_type.route(env['REQUEST_URI']) # Get the location 

如果我打印出來的FILE_TYPE我得到的輸出<route.File_type instance at 0x7f96e3e90950>

我存儲在一個字典和基於請求get_file_type返回FILE_TYPE的方法FILE_TYPE類。

path = {'html' : File_type('html', 1, 1), 
     'css' : File_type('css', 1, 0), 
     'ttf' : File_type('ttf', 0, 0), 
     } 

def get_file_type(request): # return a File_type object 
    extension = request.rsplit('.', 1)[-1] 

    if extension in path: 
     return path[extension] 

    return path['html'] 

FILE_TYPE類

class File_type: 

    def __init__(self, type_, parse, route): 
     self.type_ = type_ 
     self.parse = parse 
     self.route = route 

    def route(resource): 
     if self.route == 1: 
      for pattern, value in routes: 
       if re.search(pattern, resource): 
        return value 
      return None 
     else: 
      return resource.rsplit('.', 1)[0][1:] 
+2

請提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。你甚至沒有顯示錯誤發生在哪一行,甚至不一定提供了產生錯誤的代碼。閱讀該頁面並相應地編輯您的文章。 –

+2

初始化'File_type'時,屬性'route'覆蓋函數'route',所以你不能調用它。給他們不同的名字 –

+0

@PatrickHaugh謝謝你解決了這個問題 – Olof

回答

0

在你File_type類,你有route的方法。但是當你調用構造函數時,你會寫一些數字給self.route(這是一種方法),所以你放棄了你的方法,不能再調用它了。相反,你嘗試調用整數。所以只需更改方法或變量的名稱route

而順便說一句。方法總是必須得到self作爲參數(您的route方法不會)。

相關問題