def TypeSetter(constructor=None):
    """Returns function that takes obj, field, val and sets obj.field = val.

    constructor can be any callable that returns an object.
    """
    if constructor:

        def setter(obj, field, val):
            setattr(obj, field, constructor(val))

    else:

        def setter(obj, field, val):
            setattr(obj, field, val)

    return setter