how to return object of matching subclass?
<p>I'm trying to write a method that is supposed to return me an object of a subclass depending on some input data. Let me try to explain</p>

<pre><code>class Pet():
    @classmethod
    def parse(cls,data):
         #return Pet() if all else fails
         pass

class BigPet(Pet):
    size = "big"

    @classmethod
    def parse(cls,data):
         #return BigPet() if all subclass parsers fails
         pass        

class SmallPet(Pet):
    size = "small"

    @classmethod
    def parse(cls,data):
         #return SmallPet() if all subclass parsers fails
         pass 

class Cat(SmallPet):
    sound = "maw"

    @classmethod
    def parse(cls,data):
         #return Cat() if all criteria met
         pass 

class Dog(BigPet):
    sound = "woof"

    @classmethod
    def parse(cls,data):
         #return Dog() if all criteria met
         pass 
</code></pre>

<p>Imagine that I would like to make a "parser", such as this:</p>

<pre><code>Pet.parse(["big", "woof"])
&gt; returns object of class Dog

Pet.parse(["small", "maw"])
&gt; returns object of class Cat

Pet.parse(["small", "blup"])
&gt; returns object of class SmallPet
</code></pre>

<p>I have no idea of how to write this in a proper way. Any suggestions? Of course this is a bullshit example. I'd like to apply this on different packets of a communication protocol of some kind. </p>

<p>If i am approaching this in a completely wrong way, please tell me :)</p>

<p>Why not pass the exact class name, look for that in the <code>globals()</code> and instantiate that?</p>

<pre><code>def parse_pet(class_name, data):
    # will raise a KeyError exception if class_name doesn't exist
    cls = globals()[class_name]
    return cls(data)

cat = parse_pet('Cat', 'meow')
big_pet = parse_pet('BigPet', 'woof')
</code></pre>

python parsing design-patterns