A method is not much more than a member of a class that happens to be a function. The magic is just that when it is called on an object (a.func(x...)) the object is automatically prepended in the argument list resulting in the actual call A.func(a, x...).
What you want can be obtained that way:
class MyApp(object): # prepend with _ to "hide" the symbol def _combine_funcs(*funcs): def combined_func(*args, **kwargs): for f in funcs: f(*args, **kwargs) return combined_func def my_first_func(self): print("First") def my_second_func(self): print("Second") # actually declares combined as a method of MyClass combined = _combine_funcs(my_first_func, my_second_func)
Usage:
>>> a = MyApp() >>> a.combined() First Second
But a much cleaner way would be to use a decorator for that in Python 3:
import inspect def combine_funcs(*funcs): def outer(f): def combined_func(*args, **kwargs): # combine a bunch of functions for f in funcs: x = f(*args, *kwargs) return x # returns return value of last one combined_func.__doc__ = f.__doc__ # copy docstring and signature combined_func.__signature__ = inspect.signature(f) return combined_func return outer class MyApp(object): def my_first_func(self): print("First") def my_second_func(self): print("Second") @combine_funcs(my_first_func, my_second_func) def combined(self): """Combined function""" pass
The decorator will replace the body but will keep the docstring and the signature of the original function (in Python 3). You can then use it normally:
>>> a = MyApp() >>> a.combined() First Second >>> help(a.combined) Help on method combined_func in module __main__: combined_func() method of __main__.MyApp instance Combined function >>> help(MyApp.combined) Help on function combined_func in module __main__: combined_func(self) Combined function
self.ui.pushButton.clicked.connect(MyApp.combine_funcs(self.converter(), self.showMessage()))is it the same? They are immediately called, not waiting for a mouse click?combine_funcs(self.converter(), ...)is not the same ascombine_funcs(self.my_first_func, ...), now is it?()executing a function. This is why my init didn't do anything. I understand that now. The second problem is when I remove()inself.ui.pushButton.clicked.connect(MyApp.combine_funcs(self.converter, self.showMessage))I get an errorconverter() takes 1 positional argument but 2 were given. Converter function takes onlyselfargument. What 2 arguments am I sending?