1

I have an issue, I'd like to have a function that calls or executes all functions inside a class.

class example:
 def foo(self):
 print("hi")
 def bar(self):
 print("hello")
 def all(self):
 self.foo()
 self.bar()

Is there any better way to do this? Since my class has around 20 functions and I want just one function to call all of them. Thanks

John Coleman
52.1k7 gold badges59 silver badges127 bronze badges
asked Oct 30, 2018 at 14:59
3
  • should be possible iterate over all the methods but would not be pretty or better Commented Oct 30, 2018 at 15:06
  • You can use inspection, ugly but will work stackoverflow.com/questions/1911281/… Commented Oct 30, 2018 at 15:08
  • instead of properly trying to check the type you can try to invoke each member. Might have few issues with static or class methods if any Commented Oct 30, 2018 at 15:22

3 Answers 3

1

See How do I get list of methods in a Python class? to how enlist method with inspect or dir

While all are ugly, inspection is the prefered methods. You can invoke all the methods of an object via inspect

import inspect
class A:
 def h(self):
 print ('hellow')
 def all(self):
 for name, f in inspect.getmembers(self, predicate=inspect.ismethod):
 if name != 'all' and not name.startswith('_'):
 f()
a = A()
a.all()

If prefer dir you can try - catch getattr(self, attr)()

for attr in dir(self):
 try: 
 getattr(self, attr)()
 except Exception:
 pass
answered Oct 30, 2018 at 15:11
Sign up to request clarification or add additional context in comments.

Comments

0

Although I am not sure if that's the best way to do it, i suggest the following

class AClass():
 def my_method_1(self):
 print('inside method 1')
 def my_method_2(self):
 print('inside method 2')
def run_my_methods():
 executor = AClass()
 all_methods = dir(executor)
 #separate out the special functions like '__call__', ...
 my_methods = [method for method in all_methods if not '__' in method] 
 for method in my_methods:
 eval('executor.%s()'%method)
run_my_methods()

Output is

inside method 1 
inside method 2
answered Oct 30, 2018 at 15:25

Comments

0

I put this together and tested it and it seems to work and does not need any libraries and follows your original intent:

class example:
 def __init__(self):
 n = self.__class__.__name__
 self.method_list = [func for func in dir(eval(n)) \
 if callable(getattr(eval(n), func)) \
 and func[0]!='_' \
 and func!='run_all']
 def foo(self):
 print("hi")
 def bar(self):
 print("hello")
 def run_all(self):
 for m in self.method_list:
 runstring = 'self.' + m + '()'
 eval(runstring) 

Using it:

>> a = example()
>> a.run_all()
hello
hi

all is a Python command so I renamed your method to run_all.

func!='run_all' was necessary so you don't get a nasty recursion situation happening.

This lets you list the methods, I restricted it just so that no private methods get listed.

self.__class__.__name__ gets the name of your class

answered Oct 30, 2018 at 15:18

1 Comment

Thanks, the only problem is that it works when the function returns a print. However in my class each function returns a list of elements that with this code does not work.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.