1

My function myfunction needs custom classes as an input. I defined a lot of custom classes. I want to call myfunction with every definition of my custom classes. Is there an easy way to do this?

Basically I want to do something like this:

from custom_lib import Class_0,Class_1,....Class_100

for i in range(101):
    myfunction(Class_i)

and replace the i in the for loop so the functions gets called with all classes. Thanks in advance.

1
  • 2
    you could investigate eval? but maybe best to rethink what you're trying to achieve? thats a lot of classes! Commented Dec 3, 2019 at 9:53

2 Answers 2

2

Create a list of all the classes, and use it in the for:

class_list = [Class_0,Class_1,....Class_100] # Write the classes instead of ....
for cl in class_list:
    myfunction(cl)
Sign up to request clarification or add additional context in comments.

Comments

2
import custom_lib
for cl in dir(custom_lib):
    if cl.startswith('Class_'):
        myfunction(getattr(custom_lib, cl))

Comments

Your Answer

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.