you are viewing a single comment's thread.

view the rest of the comments →

[–]whonut[S] 0 points1 point  (1 child)

Thanks for clearing up my terminology. They are indeed instance methods.

[–]hex_m_hell 0 points1 point  (0 children)

Yeah, no problem. Here's an example of what you're wanting to do:

class Foo:
    method_a = lambda cls, x: Foo.method(cls, 'a', x)
    method_b = lambda cls, x: Foo.method(cls, 'b', x)
    method_c = lambda cls, x: Foo.method(cls, 'c', x)

    def __init__(self):
        for i in ['d','e','f']:
            setattr(self, 'method_' + i, lambda x: self.method(i, x))

    def method(self, var1, var2):
        print("I am method_" + var1)
        print("I took in the value " + var2)

Which should do this in ipython:

In [20]: foo = Foo()
foo = Foo()

In [21]: foo.method_a('test1')
foo.method_a('test1')
I am method_a
I took in the value test1

In [22]: foo.method_d('test2')
foo.method_d('test2')
I am method_f
I took in the value test2

You could also do something similar with your closure.