with open('newfoo.py', 'wb') as f:
    for name, value in list(foo.__dict__.items()):
        if ast.literal_eval(repr(value)) != value:
            raise ValueError('Tried to save {}'.format(value))
Exporting a python module (to string or py file)