2016-11-30 74 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方法不会)。

相关问题