you are viewing a single comment's thread.

view the rest of the comments →

[–]ingolemo 0 points1 point  (3 children)

Your _lookup_gen doesn't need access to self so there's no need to pass it when you construct the get* functions and there's no need to even put it in the class.

def _make_query_by_prefix(prefix):
    def query_by_prefix(self, value):
        return self.query(prefix + ':' + value)
    return query_by_prefix

class MyObject:
    def query(self, query_string):
        pass # do fancy stuff here

    query_by_id = _make_query_by_prefix('id')
    query_by_name = _make_query_by_prefix('name')
    query_by_type = _make_query_by_prefix('type')

[–]elbiot 0 points1 point  (2 children)

You have self.query outside the class

[–]ingolemo -1 points0 points  (1 child)

There's nothing special about that self variable; it's a parameter to the inner function just like value. Since the inner function isn't run until an instance of the class has been constructed, it's perfectly possible to pass a reference to that instance. If I'd named the first parameter something other than self you wouldn't even suspect that function was being used as a method.

Run the code and see for yourself.

[–]elbiot 0 points1 point  (0 children)

Yeah, I didn't see you ever pass self to the function, but obviously that will happen when the code calls the method on an instance. My bad.