-1

I have a function that return a class and I save it as a object. When I try to call on a class method I'll get the error

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

I have changed name on the object to make sure it is no naming collision.

On the first line I call the the method get_file_type in module route.

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

If I print out the file_type I get output <route.File_type instance at 0x7f96e3e90950>

I store the File_type classes in a dictionary and the method get_file_type returns a File_type based on the request.

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

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:]
Olof
  • 776
  • 2
  • 14
  • 33
  • 2
    Please provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). You haven't shown even which line the error is occuring on, nor even necessarily provided the code that produces the error. Read that page and edit your post accordingly. – Random Davis Nov 30 '16 at 21:22
  • 2
    When you initialize a `File_type`, the attribute `route` is overwriting the function `route` so you can't call it. Give them different names – Patrick Haugh Nov 30 '16 at 21:25
  • @PatrickHaugh Thanks solved the problem – Olof Nov 30 '16 at 21:35

1 Answers1

0

In your File_type class you have route as a method. But when you call constructor you write to self.route (that was a method) some integer, so you loose your method and can't call it anymore. Instead, you try to call integer. So just change the name of the method or variable route.

And btw. Method always has to get self as a parameter (your route method doesn't).

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48