Skip to content Skip to sidebar Skip to footer

How To Refer To An Instance Of A Class Using A String?

I'm a bit inexperienced with python, and I've been searching and searching but can't find a useful answer to this. I have instances of a class named tool1, tool2, etc. How do I tak

Solution 1:

Use a dictionary:

tools = {'tool1': ...,
         'tool2': ...}

my_string = 'tool1'
current_tool = tools[my_string]

Post a Comment for "How To Refer To An Instance Of A Class Using A String?"