How can I pass a method of a class as an argument to a function external to that class?

This is how it works for me:

class SomeName:
  def __init__(self):
    self.value = "something"
  def some_method(self):
    print self.value

def external_func(instance, method):
  method(instance)

external_func(SomeName(), SomeName.some_method)

      

This works correctly. Is this the correct way to do it?

+3


source to share


4 answers


Your code is "technically correct" (it does what you ask), but - at least in your example - pretty useless:

def external_func(instance, method):
  method(instance)

external_func(SomeName(), SomeName.some_method)

      

matches:

def external_func(method):
  method()

external_func(SomeName().some_method)

      



which FWIW is the same as:

SomeName().some_method()

      

but I assume you already figured that out.

Now you probably have a reason to try to pass both an AND method instance and external_func()

, or there might be a better way to solve your real problem ...

+3


source


Depending on what you are doing. Since functions are also objects in Python, this can be done.

But is this a good solution ? You seem to be trying to tackle a problem that could be better solved with a more object oriented approach:

class A:
    def __init__(self):
        self.value = "class A"
    def some_method(self):
        print self.value

class B:
    def __init__(self):
        self.value = "class B"
    def some_method(self):
        print self.value

some_class = A()
some_class.some_method()

some_class = B()
some_class.some_method()

      

Output:



"class A"
"class B"

      

In my opinion, this would be the best approach (if possible / reasonable in your case): you just call some_method()

into your class, perhaps without even knowing what type of object you mean (in relation to inheritance). The class itself knows what to do and reacts accordingly when its method has been called.

This, of course, does not work when you are working with external libraries that you do not influence.

+1


source


I certainly don't know what you are doing exactly, but it sounds to me like you are trying to do too much within a single function. Your problem might be better solved by simply separating the content external_func

.

The goals here, as I understand it, are that you don't know in advance what an object / method pair is and want to reduce code repetition.

Perhaps something like this would be better:

def main():
    obj = SomeName()
    # do the setting up portion 
    complex_object = external_func_set_up(obj)
    # presumably at some point you have to designate the method to be used:
    method = get_method_name(obj) 
    # run the method:
    getattr(obj, method)()
    # finish up the external operation: 
    external_func_complete(***args***) 

      

I realize this is more code, but I think it is much clearer at the end of what is going on and may also make you think a bit about your problem (and maybe come up with an even better solution).

+1


source


You can pass SomeName().some_method

or do some_metod

staticmethod

or classmethod

if there is no instance data in your method.

Check out the documentation to learn more about staticmethod

and classmethod

:

0


source







All Articles