2

I have the next situation. The goal of the following method is to return the object created from the incoming string. So I have:

class Situation(Generator):
    pass

And the method inside parent class:

class Generator(object):
    def createsituation(self, stringsituation="situation"):
        return "Instance of Situation"

The incoming string always equals to string "situation". Is it possible in python?

5
  • and you want to produce arbitrary class instances, or just a small set? Commented Jun 4, 2015 at 21:19
  • So, you want a class that has a method which, given a passed string, will find another class whose name matches that string, and then return an object of that matching class? Commented Jun 4, 2015 at 21:19
  • Sorry - add some corrections... Situation is child of Generator. Generator has a method which receives strings. So I want the string "situation" convert to Situation()... Commented Jun 4, 2015 at 21:21
  • 2
    Why would the method be called createsituation and the parameter stringsituation, if this is supposed to be generic? If it's only ever 'situation', why go to all the fuss?! Commented Jun 4, 2015 at 21:23
  • It's probably an XY problem anyway. Commented Jun 4, 2015 at 21:32

2 Answers 2

7

You can easily map strings to classes, yes. Classes are just more objects, you can store them in other Python objects.

You can manually build a dictionary mapping strings to classes:

classes = {'situation': Situation}

You can automate this a little by creating a class decorator, perhaps:

classes = {}
def register(cls):
    classes[cls.__name__.lower()] = cls
    return cls

@register
class Situation(Generator):
    # ...

Each class you prefix with @register will be added to the dictionary, with the class name lowercased as the key.

or you can use the globals() function to get a dictionary of all globals in your module. The latter is a little... overkill and also a security hazard, you could end up giving end-users way more access than you bargained for, as that also gives them other classes and functions you didn't mean to expose.

Once you have a dictionary, just access the right class and call it:

class Generator(object):
    def createsituation(self, stringsituation="situation"):
        return classes[stringsituation]()
Sign up to request clarification or add additional context in comments.

1 Comment

Great. This is what I want)))
0

If I understood you correctly, you want to create the Situation object from Generator's createsituation method. So you need the appropriate constructor in Situation class with passed string as an argument. Little changes in your code will achieve this:

class Situation(object):
    def __init__(self, string):
        print string

class Generator(object):
    def createsituation(self, stringsituation="situation"):
        return Situation(stringsituation)

g = Generator()
sitObj = g.createsituation("new_situation") # prints "new_situation" from Situation constructor

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.