Nam Ngo's blog

Musings of a Software Developer.

Python Class Factory

Today’s post will be on factory design pattern in Python. You can read more about it in Wiki. This design pattern allows you to create objects without specifying the exact class of object that will be created. Let’s say, you want to create an object based on a provided string.

Here are some classes. Both of them are pets. We want to dynamically create a pet from one of those classes based on a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Pony(object):

    def __init__(self, name):
        self.name = name
        self.hp = 100

    def magic_power(self):
        # Attack opponent with super powers
        pass

    def heal(self):
        # Increase hp (health point)
        pass

class Snake(object):
    # Blah blah same as Pony
    pass

Let’s write a factory method to make this happen:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def create_class(pet_class_name):
    # pet_class_name decides what class will be returned
    if pet_class_name is 'pony':
        class InheritedClass(Pony):
            # You can add additional methods or attributes if needed
            pass
        return InheritedClass
    elif pet_class_name is 'snake':
        # Or you can return the exact class
        return Snake

# Create MyPet from a string
MyPet = create_class('pony')

# MyPet is now a subclass of Pony
# It inherits everything from Pony
django = MyPet('django')

# Kaboom !!!
django.magic_power()

Comments