Question: I am using Popen to run a command but I don't know how I can write a callback that gets called once the command is finished. Any idea?

Thanks.
Bin

Answer: 
```
class Person(object):
    # ...

class Man(Person):
    # ...

class Woman(Person):
    # ...

constructors = {
    'male': Man,
    'female': Woman,
    None: Person,
}

people = {}

# in processing loop
if person.name not in people:
    people[person.name] = constructors[person.gender]()
person_object = people[person.name]

```

Since Python allows you to do things like calling storing class types in a dict, you don't need a factory; you can just look up a type and instantiate it.
The easiest way is probably to use builtin lru\_cache from functools on `__new__`.

```
import functools

class Person:
    gender = 'unknown'
    @functools.lru_cache(maxsize=None)
    def __new__(cls, full_name):
        names = first, last = full_name.split()
        for subclass in cls.__subclasses__():
            if first in subclass.names:
                cls = subclass
        self = super().__new__(cls)
        self.first, self.last = names
        return self

class Woman(Person):
    gender = 'female'
    names = {*'Mary Linda Susan'.split()}

class Man(Person):
    gender = 'male'
    names = {*'Tom Dick Harry'.split()}

```